python - pandas 中的动态滚动功能
问题描述
我正在尝试在 Pandas中实现动态移动平均线std
和其他动态移动函数。max
“正常”和动态移动平均线之间的区别在于边缘的行为。这主要改善了两件事:
- 返回的数据不会像“正常”移动平均线那样移动
- 返回的数据覆盖输入数据的整个范围。使用“正常”移动平均线,根据移动平均线的窗口大小裁剪返回的数据范围。
因此,要获得动态移动平均线,有多种方法,如反射、第一个/最后一个值的重复…… 对我来说,反射方法就足够了。这和其他行为在过滤器中实现scripy.ndimage.filters
,例如uniform_filter1d
. 我已经查看了 Pandas 文档,但是rolling
我找不到任何改变边缘行为的参数......我自己添加这个似乎是可行的,但我问这个问题,看看我是否已经在 Pandas 中有一个实现错过了。
所以我的问题可以简化为:有没有办法rolling
像我描述的那样动态使用 Pandas?
“正常”移动平均线:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import filters
def moving_avg(series: pd.Series, window: int) -> pd.Series:
return series.rolling(window=window).mean()
t = np.linspace(0, 100, 1000)
y = np.sin(2 * np.pi * 0.03 * t)
filtered_y = filters.uniform_filter1d(y, 200)
pd_y = pd.Series(y, name="Pandas Moving Average")
pd_y = moving_avg(pd_y, 200) # Using a large window on purpose to show the shift!
plt.plot(y, label="Raw Data")
plt.plot(filtered_y, label="uniform_filter1d")
pd_y.plot()
plt.legend()
plt.show()
结果
那么有什么办法可以解决这个问题吗?
解决方案
我不认为有你想要实现的东西。一种简单的方法是使用concat
添加边缘值来创建反射效果。在一个函数中,它可能类似于
def rolling_reflect_center(s, window):
nb_ref = window // 2
rolling_ = pd.concat(
[pd.Series(s.iloc[:nb_ref].values)[::-1],#reflect on the left
s,
pd.Series(s.iloc[-nb_ref+1:].values[::-1]) #reflect on the right
]).rolling(window ,center=True)
return rolling_
然后你做
filtered_y = filters.uniform_filter1d(y, 200)
pd_y = pd.Series(y, name="Pandas Moving Average")
pd_y = rolling_reflect_center( pd_y, window=200).mean()
print(np.allclose(pd_y.dropna().to_numpy(),filtered_y))
#True
情节也很好,因为它会nan
自动放弃
推荐阅读
- azure-application-insights - 如何将getschema 用于customDimensions?
- pyspark - 复杂数据管道迁移计划问题
- python - 如何使用 pandas 计算错误百分比(groupby)?
- python - 在 sqlalchemy 中查找平均日期
- java - 如何使用随机化,因此当您按下按钮时,它会将您带到随机屏幕
- python - 在 Python 中创建随机变量,数组的三分之一为零
- python - 用索引矩阵检索值的Pythonic方式?
- python - 从连续变量 Python 创建多个类
- java - 片段中的 FileInputStream 给出 FileNotFound 异常
- redis - 如何在 Kubernetes 中为 redis 服务配置 envoy 边缘代理?