Python 2.7、spyder、Anaconda 安装的所有包。升级到 pandas 0.20.3 之前代码运行良好
尝试使用 unicode 字符串日期“2017-09-13”索引两个数据帧,days 和 udly
他们都有相同的索引
udly.index[-1]
Out[13]: Timestamp('2017-09-13 00:00:00')
days.index[-1]
Out[14]: Timestamp('2017-09-13 00:00:00')
在很好地索引数据帧时,一切顺利
udly['2017-09-13']
Out[15]:
CLOSE VOLUME ytm_1 ATMstk
2017-09-13 2.739 307060583.0 3.4912 2.75
但是当索引数据框天时,事情变得棘手
days['2017-09-13']
Traceback (most recent call last):
...........
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: '2017-09-13'
更新
使用 .loc 进行索引有效,但返回给我 Pandas.Series,但我真的很想知道发生这种情况的原因,为什么我无法索引数据帧。
type(udly['2017-09-13'])
Out[23]: pandas.core.frame.DataFrame
type(days.loc['2017-09-13'])
Out[24]: pandas.core.series.Series
更棘手的部分是我可以使用部分索引(例如 '2017-09'
days['2017-09']
Out[39]:
2017-09-11 2017-09-11
2017-09-12 2017-09-12
2017-09-13 2017-09-13