XLDateAmbiguous变通方法

 李东润 发布于 2023-02-12 18:28

将Excel文件读入Python通常意味着绊倒Excel闰年问题.这在许多帖子中都有描述,但没有一个提供方便的解决方案.所以这就是我在这里问的问题.使用以下代码:

import xlrd
from pandas import *

xlfile = 'test.xlsx'
wb = xlrd.open_workbook(xlfile)

sn = wb.sheet_names()
dfs = [read_excel(xlfile, x) for x in sn]

怎么可以避免由此产生的问题*:

---------------------------------------------------------------------------
XLDateAmbiguous                           Traceback (most recent call last)
 in ()
      1 sn = wb.sheet_names()
      2 
----> 3 dfs = [read_excel(xlfile, x) for x in sn]

/R/.virtualenv/pydata/lib/python2.7/site-packages/pandas/io/excel.pyc in read_excel(path_or_buf, sheetname, kind, **kwds)
     50     """
     51     return ExcelFile(path_or_buf,kind=kind).parse(sheetname=sheetname,
---> 52                                                   kind=kind, **kwds)
     53 
     54 class ExcelFile(object):

/R/.virtualenv/pydata/lib/python2.7/site-packages/pandas/io/excel.pyc in parse(self, sheetname, header, skiprows, skip_footer, index_col, parse_cols, parse_dates, date_parser, na_values, thousands, chunksize, **kwds)
    138                                      chunksize=chunksize,
    139                                      skip_footer=skip_footer,
--> 140                                      **kwds)
    141 
    142     def _should_parse(self, i, parse_cols):

/R/.virtualenv/pydata/lib/python2.7/site-packages/pandas/io/excel.pyc in _parse_excel(self, sheetname, header, skiprows, skip_footer, index_col, has_index_names, parse_cols, parse_dates, date_parser, na_values, thousands, chunksize, **kwds)
    194                 if parse_cols is None or should_parse[j]:
    195                     if typ == XL_CELL_DATE:
--> 196                         dt = xldate_as_tuple(value, datemode)
    197                         # how to produce this first case?
    198                         if dt[0] < datetime.MINYEAR:  # pragma: no cover

/R/.virtualenv/pydata/lib/python2.7/site-packages/xlrd/xldate.pyc in xldate_as_tuple(xldate, datemode)
     78 
     79     if xldays < 61 and datemode == 0:
---> 80         raise XLDateAmbiguous(xldate)
     81 
     82     jdn = xldays + _JDN_delta[datemode]

XLDateAmbiguous: 1.0

*除了在输入任何数据或使用NA搜索/替换1/1/1900之前在Excel中手动更改日期系统 ...

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有