Pandas monthly rolling operation

I ended up understanding this by writing out this question, so I will just send a message and answer my question if someone needs a little help.

Problem

Suppose that we have DataFrame, dfthat contain the data.

import pandas as pd
from io import StringIO

data = StringIO(
"""\
date          spendings  category
2014-03-25    10         A
2014-04-05    20         A
2014-04-15    10         A
2014-04-25    10         B
2014-05-05    10         B
2014-05-15    10         A
2014-05-25    10         A
"""
)

df = pd.read_csv(data,sep="\s+",parse_dates=True,index_col="date")

purpose

For each line, summarize spendingsfor each line that is within one month , ideally using DataFrame.rolling, since this is a very clean syntax.

What i tried

df = df.rolling("M").sum()

But this throws an exception

ValueError: <MonthEnd> is a non-fixed frequency

version: pandas==0.19.2

+6
source share
2 answers

"D", "M", "30D" 30 .

df = df.rolling("30D").sum()

, "M", , , , .

+11

, , "AS" "Y", "Y" "", YearEnd (http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases), (, 365 , 1 1 , 31 ).

( 30D) , . , , .

( ):

df['Sum'] = [
    df.loc[
        edt - pd.tseries.offsets.DateOffset(months=1):edt, 'spendings'
    ].sum() for edt in df.index
]
spendings   category    Sum
date            
2014-03-25  10  A   10
2014-04-05  20  A   30
2014-04-15  10  A   40
2014-04-25  10  B   50
2014-05-05  10  B   50
2014-05-15  10  A   40
2014-05-25  10  A   40
+4

Source: https://habr.com/ru/post/1016691/


All Articles