下面是我在Pandas 身上的离群点检测代码.我正在滚动窗口15,我想要做的是在窗口5,其中这个窗口是基于星期几的中心日期,即,如果中心是星期一,采取2倒退星期一和2向前星期一.滚动版对此没有任何支持.怎么办?

import pandas as pd
import numpy as np

np.random.seed(0)

dates = pd.date_range(start='2022-01-01', end='2023-12-31', freq='D')

prices1 = np.random.randint(10, 100, size=len(dates))
prices2 = np.random.randint(20, 120, size=len(dates)).astype(float)

data = {'Date': dates, 'Price1': prices1, 'Price2': prices2}
df = pd.DataFrame(data)

r = df.Price1.rolling(window=15, center=True)
price_up, price_low = r.mean() + 2 * r.std(), r.mean()  -  2 * r.std()

mask_upper = df['Price1'] > price_up
mask_lower = df['Price1'] < price_low

df.loc[mask_upper, 'Price1'] = r.mean()
df.loc[mask_lower, 'Price1'] = r.mean()

推荐答案

一种 Select 使用groupby.rollingdayofweek作为分组,以确保在滚动中只使用相同的日期:

r = (df.set_index('Date')
       .groupby(df['Date'].dt.dayofweek.values) # avoid index alignment
       .rolling(f'{5*7}D', center=True)
       ['Price1']
    )
avg = r.mean().set_axis(df.index) # restore correct index
std = r.std().set_axis(df.index)
price_up, price_low = avg + 2 * std, avg  -  2 * std

mask_upper = df['Price1'] > price_up
mask_lower = df['Price1'] < price_low

df.loc[mask_upper, 'Price1'] = avg
df.loc[mask_lower, 'Price1'] = avg

输出示例:

          Date  Price1  Price2
0   2022-01-01    54.0    86.0
1   2022-01-02    57.0   117.0
2   2022-01-03    74.0    32.0
3   2022-01-04    77.0    35.0
4   2022-01-05    77.0    53.0
..         ...     ...     ...
725 2023-12-27    44.0    37.0
726 2023-12-28    60.0    65.0
727 2023-12-29    30.0   116.0
728 2023-12-30    53.0    82.0
729 2023-12-31    10.0    42.0

[730 rows x 3 columns]

Python相关问答推荐

使用mySQL的SQlalchemy过滤重叠时间段

韦尔福德方差与Numpy方差不同

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

Excel图表-使用openpyxl更改水平轴与Y轴相交的位置(Python)

如何找到满足各组口罩条件的第一行?

如何记录脚本输出

C#使用程序从Python中执行Exec文件

如何请求使用Python将文件下载到带有登录名的门户网站?

如何使用pytest来查看Python中是否存在class attribution属性?

DataFrames与NaN的条件乘法

梯度下降:简化要素集的运行时间比原始要素集长

合并与拼接并举

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

替换现有列名中的字符,而不创建新列

python sklearn ValueError:使用序列设置数组元素

如果包含特定值,则筛选Groupby

将字节序列解码为Unicode字符串

为什么在不先将包作为模块导入的情况下相对导入不起作用

如何在Django查询集中生成带有值列表的带注释的字段?

使用元组扩展字典的产品挑战