使用.loc()时的Pandas KeyError

时间:2020-06-14 04:12:48

标签: python pandas dataframe

我有一个熊猫DataFrame 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]

1 个答案:

答案 0 :(得分:0)

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

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