熊猫位置问题,无法在索引中找到日期值

时间:2019-03-16 21:12:43

标签: python pandas

我有下一个测试数据CSV文件

time,value
2018-09-17,0.0002897
2018-09-16,0.0002955
2018-09-15,0.0002862
2018-09-14,0.0003054
2018-09-13,0.000312
2018-09-12,0.0003467
2018-09-11,0.0003433
2018-09-10,0.0003287
2018-09-09,0.0003355
2018-09-08,0.0003288

当我在python控制台中运行下一个代码时,结果熊猫在索引中找不到第一行(2018-09-17)。您可以在下面的成绩单中看到错误消息

>>> import pandas as pd
>>> s1 = pd.read_csv("series1.csv")
>>> s1['time'] = pd.to_datetime(s1['time'])
>>> s1.set_index('time', inplace=True)
>>> s1
               value
time
2018-09-17  0.000290
2018-09-16  0.000296
2018-09-15  0.000286
2018-09-14  0.000305
2018-09-13  0.000312
2018-09-12  0.000347
2018-09-11  0.000343
2018-09-10  0.000329
2018-09-09  0.000336
2018-09-08  0.000329
>>> s1.loc['2018-09-16']
               value
time
2018-09-16  0.000296
>>> s1.loc['2018-09-17']
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\pandas\core\indexing.py", line 1506, in _has_valid_type
    error()
  File "C:\Python36\lib\site-packages\pandas\core\indexing.py", line 1501, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [2018-09-17] is not in the [index]'

我在Linux和Windows上重复了此测试,结果相同。在Windows和Linux上,熊猫的版本均为0.22.0

1 个答案:

答案 0 :(得分:0)

我将错误报告发布到pandas-dev上,他们回答说,此错误已在pandas 0.24.2中修复。