python - '不再支持将列表喜欢传递给带有任何缺失标签的 .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)
解决方案
你可以尝试这样的事情:
result = df.loc[df.index.intersection(ix)]
推荐阅读
- ruby-on-rails - 在 Map 函数中的符号内传递方法
- java - Drools/Kie-Server/Busines-Central 7.28.0 当 kie-server 访问 business-central 上的 websocket 控制器时收到 403
- racket - Racket - 在二维列表中查找具有相同元素的列表
- vue.js - 登录后 Vue 仅渲染我的 App.vue 的一部分
- r - 删除函数中导致它在 R 中停止的元素
- spring - 如何在 Spring Boot 2.1.9 中配置 ssl 证书
- windows - 基本 Bash 脚本导致“edge.sh: line 13: npm: command not found”在此处发现问题,但未解决
- javascript - 使用 Javascript 将表行从一个表移动到另一个表
- php - 在 Laravel 中使用 SQL 进行连接内连接和记录计数
- google-photos - Google Photos API:识别“动态照片”