首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python警告:将对布尔序列键进行重新索引以匹配DataFrame索引

python警告:将对布尔序列键进行重新索引以匹配DataFrame索引
EN

Stack Overflow用户
提问于 2019-08-31 21:24:02
回答 1查看 176关注 0票数 0

下面的代码报告了一个警告:

代码语言:javascript
运行
复制
UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  ds = ds[(df[['ts']].diff() > threshold).any(axis=1)]
2019-08-31 08:18:57.731 python[58541:1317145] [QL] Can't get plugin bundle info at file:///Users/e12714/Library/QuickLook/NSQuickLookPlugin.qlgenerator

代码:

代码语言:javascript
运行
复制
#!/usr/bin/env python
import pandas as pd  
import numpy as np
from datetime import datetime,timedelta
import matplotlib.pyplot as plt
from collections import OrderedDict

m = OrderedDict()
m["08-30 22:30:10.063"] = 5
m["08-30 22:30:15.023"] = 5
m["08-30 22:30:20.043"] = 5
m["08-30 22:30:25.015"] = 2
m["08-30 22:30:25.020"] = 2
m["08-30 22:30:26.025"] = 2
m["08-30 22:30:40.032"] = 5
m["08-30 22:30:45.045"] = 5
m["08-30 22:30:50.022"] = 5

df = pd.DataFrame(list(m.items()), columns = ['ts', 'value'])
df['ts'] = [datetime.strptime(x,'%m-%d %H:%M:%S.%f') for x in df['ts']]
plt.style.use('ggplot')
fig, ax = plt.subplots(figsize=(12,6))
ax.plot(df['ts'],df['value'],"--.")
dl = df[(df[['value']].shift() != df[['value']]).any(axis=1)]
dr = df[(df[['value']].shift(-1) != df[['value']]).any(axis=1)]
ds = pd.concat([dl,dr],ignore_index=True)
ds = ds.sort_values(['ts'])
threshold = timedelta(seconds=2) 
ds = ds[(df[['ts']].diff() > threshold).any(axis=1)]
fig.autofmt_xdate()
ax.xaxis.set_ticks(np.array(ds['ts']))
ax.yaxis.grid(True)
plt.show()

输出听起来不错:

如何修复此警告?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-31 22:10:07

更改以下代码行

代码语言:javascript
运行
复制
ds = ds[(df[['ts']].diff() > threshold).any(axis=1)]

代码语言:javascript
运行
复制
ds = ds[(ds[['ts']].diff() > threshold).any(axis=1)]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57738381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档