我有这样的数据(索引是时间戳,所以它是日期时间类型):
timestamp price quantity direction aggregated
2020-09-04 00:00:00.000 381.501760 3.000 s True
2020-09-04 00:00:00.212 381.530000 3.225 s False
2020-09-04 00:00:00.560 381.346627 207.477 s True
2020-09-04 00:00:00.590 381.450000 2.586 s False
2020-09-04 00:00:00.652 381.540000 0.030 b False
...
我想在价格上做一个滚动窗口,但只在汇总= True的行上
df[df['aggregated'] == True]['price'].rolling("25s", min_periods=1).mean()
我用来绘制的代码是这样的:
fig, (ax1, ax2) = plt.subplots(2, figsize=(20, 10), sharex='all', gridspec_kw={'height_ratios': [4, 1]}, constrained_layout=True)
p0=ax1.plot(df.index, df['price'], color='darkorange', zorder = 0)
p1=ax2.plot(df.index, df[df['aggregated'] == True]['price'].rolling("25s", min_periods=1).mean(), color='blue')
但出现以下错误:
x和y必须具有相同的第一尺寸,但形状为(487452,)和(188252,)
如果删除聚合列上的测试,则会得到以下信息:
fig, (ax1, ax2) = plt.subplots(2, figsize=(20, 10), sharex='all', gridspec_kw={'height_ratios': [4, 1]}, constrained_layout=True)
p0=ax1.plot(df.index, df['price'], color='darkorange', zorder = 0)
p1=ax2.plot(df.index, df['price'].rolling("25s", min_periods=1).mean(), color='blue')
而且有效。所以我想这不喜欢缺少某些行。
我该如何工作?
问题在于,.plot
期望两个数组的大小相同,并且您要通过传递一个更长的数组df.index
。
尝试
filtered = df[df['aggregated'] == True]['price'].rolling("25s", min_periods=1).mean()
...
p1 = ax2.plot(filtered.index, filtered.price, color='blue')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句