Pandas - 滚动斜率计算

Pandas - Rolling slope calculation

如何计算每列的rolling(window=60)值的斜率,步长为5?

我想每 5 分钟计算一次值,我不需要每条记录的结果。

这是示例数据框和结果:

df
Time                A    ...      N
2016-01-01 00:00  1.2    ...    4.2
2016-01-01 00:01  1.2    ...    4.0
2016-01-01 00:02  1.2    ...    4.5
2016-01-01 00:03  1.5    ...    4.2
2016-01-01 00:04  1.1    ...    4.6
2016-01-01 00:05  1.6    ...    4.1
2016-01-01 00:06  1.7    ...    4.3
2016-01-01 00:07  1.8    ...    4.5
2016-01-01 00:08  1.1    ...    4.1
2016-01-01 00:09  1.5    ...    4.1
2016-01-01 00:10  1.6    ...    4.1
....

result
Time                A    ...      N
2016-01-01 00:04  xxx    ...    xxx
2016-01-01 00:09  xxx    ...    xxx
2016-01-01 00:14  xxx    ...    xxx
...

df.rolling函数可以解决这个问题吗?

如果 NaN 在 window 中也没关系,这意味着子集可以小于 60。

您可以使用 pandas Resample。请注意,要使用它,您需要一个具有时间值的索引

df.index = pd.to_datetime(df.Time)
print df
result = df.resample('5Min').bfill()
print result
                                 Time    A    N
Time                                           
2016-01-01 00:00:00  2016-01-01 00:00  1.2  4.2
2016-01-01 00:01:00  2016-01-01 00:01  1.2  4.0
2016-01-01 00:02:00  2016-01-01 00:02  1.2  4.5
2016-01-01 00:03:00  2016-01-01 00:03  1.5  4.2
2016-01-01 00:04:00  2016-01-01 00:04  1.1  4.6
2016-01-01 00:05:00  2016-01-01 00:05  1.6  4.1
2016-01-01 00:06:00  2016-01-01 00:06  1.7  4.3
2016-01-01 00:07:00  2016-01-01 00:07  1.8  4.5
2016-01-01 00:08:00  2016-01-01 00:08  1.1  4.1
2016-01-01 00:09:00  2016-01-01 00:09  1.5  4.1
2016-01-01 00:10:00  2016-01-01 00:10  1.6  4.1
2016-01-01 00:15:00  2016-01-01 00:15  1.6  4.1
                                 Time    A    N

输出

Time                                           
2016-01-01 00:00:00  2016-01-01 00:00  1.2  4.2
2016-01-01 00:05:00  2016-01-01 00:05  1.6  4.1
2016-01-01 00:10:00  2016-01-01 00:10  1.6  4.1
2016-01-01 00:15:00  2016-01-01 00:15  1.6  4.1

试试这个

windows = df.groupby("Time")["A"].rolling(60)
df[out] = windows.apply(lambda x: np.polyfit(range(60), x, 1)[0], raw=True).values

看来你想要的是以特定步长滚动。 但是,根据 documentation of pandasrolling.

目前 不支持 步长

如果数据量不是太大,只需对所有数据执行滚动,select使用索引得到结果。

这是一个示例数据集。为简单起见,时间列使用整数表示。

data = pd.DataFrame(np.random.rand(500, 1) * 10, columns=['a'])
            a
0    8.714074
1    0.985467
2    9.101299
3    4.598044
4    4.193559
..        ...
495  9.736984
496  2.447377
497  5.209420
498  2.698441
499  3.438271

然后,滚动并计算坡度,

def calc_slope(x):
    slope = np.polyfit(range(len(x)), x, 1)[0]
    return slope

# set min_periods=2 to allow subsets less than 60.
# use [4::5] to select the results you need.
result = data.rolling(60, min_periods=2).apply(calc_slope)[4::5]

结果会是,

            a
4   -0.542845
9    0.084953
14   0.155297
19  -0.048813
24  -0.011947
..        ...
479 -0.004792
484 -0.003714
489  0.022448
494  0.037301
499  0.027189

或者,您可以参考这个post。第一个答案提供了一种实现此目的的 numpy 方法: step size in pandas.DataFrame.rolling

我使用:

    df['slope_I'] = df['I'].rolling('600s').apply(lambda x: (x[-1]-x[0])/600) 

其中斜率以 1/秒为单位。

可能结果的前600s是空的,你应该用零填充它,或者用平均值。 斜率列中的第一个数字将是从 window 内的第一行到最后一行的直线的斜率,在滚动过程中依此类推。

此致。

对于其他寻求答案的人,这里我得到了另一种解决方案,其中时间间隔不需要相同的长度。

df.A.diff(60)/df.Time.diff(60).dt.total_seconds()

这行代码将当前行与六十行的差异除以相同行的时间差异。 当您只需要每五个记录时,下一行应该可以工作。

df.A.diff(60)/df.Time.diff(60).dt.total_seconds()[4::5]

注意:计算每一行,只返回第 5 步序列

文档 pandas 差异:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.diff.html