T Denager
T Denager

Reputation: 37

How to resample yearly starting from 1st of June to 31st may?

How do I resample a dataframe with a daily time-series index to yearly, but not from 1st Jan to 31th Dec. Instead I want the yearly sum from 1.June to 31.May.

First I did this, which gives me the yearly sum from 1.Jan to 31.Dec:

df.resample(rule='A').sum()

I have tried using the base-parameter, but it does not change the resample sum.

df.resample(rule='A', base=100).sum()

Here is a part of my dataframe:

In []: df
Out[]:
Index                ET        P         R
2010-01-01 00:00:00  -0.013    0.0       0.773
2010-01-02 00:00:00  0.0737    0.21      0.797
2010-01-03 00:00:00  -0.048    0.0       0.926
...

In []: df.resample(rule='A', base = 0, label='left').sum()

Out []: 
Index
2009-12-31 00:00:00  424.131138   871.48  541.677405
2010-12-31 00:00:00  405.625780   939.06  575.163096
2011-12-31 00:00:00  461.586365  1064.82  710.507947
...

I would really appreciate if anyone could help me figuring out how to do this.

Thank you

Upvotes: 1

Views: 3895

Answers (1)

root
root

Reputation: 33793

Use 'AS-JUN' as the rule with resample:

# Example data
idx = pd.date_range('2017-01-01', '2018-12-31')
s = pd.Series(1, idx)

# Resample
s = s.resample('AS-JUN').sum()

The resulting output:

2016-06-01    151
2017-06-01    365
2018-06-01    214
Freq: AS-JUN, dtype: int64

Upvotes: 4

Related Questions