首页 > 解决方案 > 使用 .loc() 时的 Pandas KeyError

问题描述

我有一个熊猫数据框portfolio,其键是日期。我正在尝试通过访问多行

print(portfolio.loc[['2007-02-26','2008-02-06'],:]),

但我收到一个错误

KeyError: "None of [Index(['2007-02-26', '2008-02-06'], dtype='object', name='Date')] are in the [index]"

但是,print(portfolio.loc['2007-02-26',:])成功返回

holdings      1094.6124
pos_diff       100.0000
cash         98905.3876
total       100000.0000
returns          0.0000
Name: 2007-02-26 00:00:00, dtype: float64

这不是有效的格式-->df.loc[['key1', 'key2', 'key3'], 'Column1]吗?

标签: pythonpandasdataframe

解决方案


似乎问题在于从字符串到时间戳的类型转换。因此,解决方案是将标签集显式转换为 DateTime,然后再将它们传递给loc

df = pd.DataFrame({"a" : range(5)}, index = pd.date_range("2020-01-01", freq="1D", periods=5))
print(df) 

==> 
            a
2020-01-01  0
2020-01-02  1
2020-01-03  2
2020-01-04  3
2020-01-05  4

try:
    df.loc[["2020-01-01", "2020-01-02"], :]    
except Exception as e:
    print (e) 

 ==>
"None of [Index(['2020-01-01', '2020-01-02'], dtype='object')] are in the [index]"

# But - if you convert the labels to datetime before calling loc, 
# it works fine. 
df.loc[pd.to_datetime(["2020-01-01", "2020-01-02"]), :]

===>
            a
2020-01-01  0
2020-01-02  1

推荐阅读