Python警告:布尔系列键将重新索引以匹配DataFrame索引

2024-04-19 01:13:33 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码报告警告:

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

代码:

#!/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()

输出听起来不错:

enter image description here

如何修复此警告


Tags: 代码import警告dfdatetimethresholdvalueas