大 pandas 整周的窗口 [英] Window of full weeks in pandas

查看:63
本文介绍了大 pandas 整周的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个特殊的

当然,我可以使用 for 循环等手动执行操作,但我想现有的 Pandas 机制可用于执行此操作...?

解决方案

Pandas 自定义窗口滚动

另一种用法此处

将pandas导入为pd将 numpy 导入为 np从 pandas.api.indexers 导入 BaseIndexer从输入导入可选,元组类 CustomIndexer(BaseIndexer):def get_window_bounds(self,num_values: int = 0,min_periods:可选 [int] = 无,中心:可选 [bool] = 无,关闭:可选 [str] = 无) ->元组[np.ndarray, np.ndarray]:end = np.arange(1, num_values+1, dtype=np.int64)开始 = 结束 % 7返回开始,结束

indexer = CustomIndexer(num_values=len(s))s.rolling(indexer).mean().round(2)

输出:

2020-01-01 NaN2020-01-02 NaN2020-01-03 NaN2020-01-04 NaN2020-01-05 NaN2020-01-06 NaN2020-01-07 3.002020-01-08 3.142020-01-09 3.292020-01-10 3.432020-01-11 3.292020-01-12 3.432020-01-13 3.572020-01-14 3.362020-01-15 3.362020-01-16 3.362020-01-17 3.362020-01-18 3.362020-01-19 3.362020-01-20 3.362020-01-21 3.242020-01-22 3.33频率:D,数据类型:float64

I am looking for a special window function in pandas: sort of a combination of rolling and expanding. For calculating (for instance) the mean and standard deviating, I want to regard all past data, but ignore the first few records to make sure I have a multiple of 7 (days in my case). That's because I know the data has a strong weekly pattern.

Example:

s = pd.Series([1, 3, 4, 5, 4, 3, 1, 2, 4, 5, 4, 5, 4, 2, 1, 3, 4, 5, 4, 3, 1, 3],
              pd.date_range('2020-01-01', '2020-01-22'))
s.rolling(7, 7).mean()   # Use last 7 days.
s.expanding(7).mean()    # Use all past days.
s.mywindowing(7).mean()  # Use last past multiple of 7 days. How?

The effect should be like this:

Of course I can do things manually using for loops and such, but I imagine the existing pandas machinery can be used to do this...?

解决方案

Pandas custom window rolling

another usage here

import pandas as pd
import numpy as np
from pandas.api.indexers import BaseIndexer
from typing import Optional, Tuple


class CustomIndexer(BaseIndexer):
    def get_window_bounds(self,
                          num_values: int = 0,
                          min_periods: Optional[int] = None,
                          center: Optional[bool] = None,
                          closed: Optional[str] = None
                          ) -> Tuple[np.ndarray, np.ndarray]:

        end = np.arange(1, num_values+1, dtype=np.int64)
        start = end % 7
        return start, end

indexer = CustomIndexer(num_values=len(s))
s.rolling(indexer).mean().round(2)

Outputs:

2020-01-01     NaN
2020-01-02     NaN
2020-01-03     NaN
2020-01-04     NaN
2020-01-05     NaN
2020-01-06     NaN
2020-01-07    3.00
2020-01-08    3.14
2020-01-09    3.29
2020-01-10    3.43
2020-01-11    3.29
2020-01-12    3.43
2020-01-13    3.57
2020-01-14    3.36
2020-01-15    3.36
2020-01-16    3.36
2020-01-17    3.36
2020-01-18    3.36
2020-01-19    3.36
2020-01-20    3.36
2020-01-21    3.24
2020-01-22    3.33
Freq: D, dtype: float64

这篇关于大 pandas 整周的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆