Home > Mobile >  Pandas series: find the sum of values in a period of one month each time
Pandas series: find the sum of values in a period of one month each time

Time:03-31

Assume we have the series:

2015-12-19 00:00:00 0.02347
2015-12-19 01:00:00 0.02152
2015-12-19 02:00:00 0.02329
2015-12-19 03:00:00 0.02045
2015-12-19 04:00:00 0.01962
2015-12-19 05:00:00 0.01896
2015-12-19 06:00:00 0.02184
2015-12-19 07:00:00 0.0239
2015-12-19 08:00:00 0.02928
2015-12-19 09:00:00 0.03008
2015-12-19 10:00:00 0.03007
.
.
.
2016-08-18 23:00:00 0.02892

I want to find the sum of the values every month but not from the 1st of each month. I want to find for example the sum of values:
from 19-12-2015 to 18-1-2016
from 19-1-2016 to 18-2-2016
etc.

CodePudding user response:

How about this?

import pandas as pd
import io   

data = """2015-12-19 00:00:00;0.02347
2015-12-19 01:00:00; 0.02152
2015-12-19 02:00:00; 0.02329
2015-12-19 03:00:00; 0.02045
2015-12-19 04:00:00; 0.01962
2015-12-19 05:00:00; 0.01896
2015-12-19 06:00:00; 0.02184
2015-12-19 07:00:00; 0.0239
2015-12-19 08:00:00; 0.02928
2016-12-19 09:00:00; 0.03008
2016-12-20 10:00:00; 0.03007"""

df = pd.read_csv(io.StringIO(data), sep=";", header=None, index_col=0, parse_dates=True)

# subtract 19 days
df.index = df.index - pd.Timedelta(days=19)
# sum months
df = df.groupby(pd.Grouper(freq='M')).sum()
# apply offsets again
df.index = df.index   pd.Timedelta(days=19)
  • Related