首页 > 解决方案 > '不再支持将列表喜欢传递给带有任何缺失标签的 .loc 或 [],请参阅

问题描述

如何修复错误:

KeyError: 'Passing list-likes to .loc or [] with any missing labels 
is no longer supported, see 
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'

我只想获取索引“ix”中的数据框的位置。

这是一个可重现的样本:

import pandas as pd
ix = pd.DatetimeIndex(["2019-07-19 08:47:00", "2019-07-19 08:48:00", "2019-07-19 08:49:00", '2019-07-19 13:43:00', '2019-07-19 13:44:00', '2019-07-19 13:45:00', '2019-07-19 13:46:00', '2019-07-19 13:47:00', '2019-07-19 13:48:00', '2019-07-19 13:49:00', '2019-07-19 13:50:00', '2019-07-19 13:51:00', '2019-07-19 13:52:00'], dtype='datetime64[ns]', name='Time', freq=None)
df = pd.DataFrame({"result":[7.445043,0.585584,1.735565,3.217186, 0.211871,0.000000,0.180448,21.645403,22.724170,304.292450]}, index = [
"2019-07-19 08:47:00", "2019-07-19 08:48:00", "2019-07-19 08:49:00", "2019-07-19 08:50:00", "2019-07-19 08:51:00", "2019-07-19 08:52:00",  "2019-07-19 08:53:00", "2019-07-19 08:54:00",   "2019-07-19 08:55:00",  "2019-07-19 08:56:00"   ])
df.index = pd.to_datetime(df.index)

result = df.loc[ix]
print(result)

标签: pythonpandasdataframedatetimeerror-handling

解决方案


你可以尝试这样的事情:

result = df.loc[df.index.intersection(ix)]


推荐阅读