KeyError:“ [index(['2019-06-06','2019-06-10'],dtype ='object')]都不存在

时间:2020-05-31 11:25:23

标签: pandas

下面给出错误KeyError: "None of [Index(['2019-06-06', '2019-06-10'], dtype='object')] are in the [index]",这是什么问题?

test_data = pd.Series([1,2,3,4,5,6], index=['20190606','20190610','20190708','20190805','20190909','20190930'])
test_data.index=pd.to_datetime(test_data.index)

print(test_data.loc[['2019-06-06','2019-06-10']])

1 个答案:

答案 0 :(得分:1)

没有匹配项,因为熊猫默认情况下无法将日期时间列表转换为日期时间,因此必须先进行转换:

print(test_data.loc[pd.to_datetime(['2019-06-06','2019-06-10'])])
2019-06-06    1
2019-06-10    2
dtype: int64