hey_arnold
hey_arnold

Reputation: 141

Using pandas resample monthly data to yearly data but start from a certain month

How do I resample monthly data to yearly data but starting from 1st October.

I tried the following as I know using base works for starting at a certain hour of a day but doesnt appear to work for month of the year.

df = (df.resample(rule='Y',  base=10).sum().reset_index())

Upvotes: 1

Views: 2621

Answers (2)

Swier
Swier

Reputation: 4186

Pandas has anchored offsets available for annual resamples starting at the first of a month.

The anchored offset for annual resampling starting in October is AS-OCT. Resampling and summing can be done like this:

df.resample("AS-OCT").sum()

Upvotes: 2

hey_arnold
hey_arnold

Reputation: 141

Here is how you do it:

offset = pd.DateOffset(months=9) 

df.shift(freq=-offset).resample('YS').sum().shift(freq=offset)

Upvotes: 2

Related Questions