将Excel文件读取到Python中通常意味着跳过the Excel leap year issue。许多帖子都对此进行了描述,但是没有一个提供方便的解决方案。这就是我在这里要问的。使用以下代码:
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)
<ipython-input-8-1db99305e2ac> in <module>()
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
*除changing the date system manually in Excel之外,然后再输入任何数据或用NA搜索/替换1900年1月1日...
最佳答案
我已经成功地做到了:
#Set local time to dataframe index
dat['local_time']=pd.to_datetime(dat[local_time_column_name], format=date_format)
dat=dat.set_index('local_time')
dat=dat.tz_localize(timezone, ambiguous='infer')
将时区未知的日期时间设置为数据帧索引,然后使用ambiguous ='infer'标志。
关于python - XLDateAmbiguous解决方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20391888/