简体   繁体   中英

Group time series by a custom fixed period

I want to group my time series by a fixed period, exactly the same behavior as the function to_period() but with a custom period.

# Some test data
df = pd.DataFrame({'categ': np.random.choice([1,2,3], 12)}, index=pd.date_range(start=pd.to_datetime('2015-01-01'),periods=12, freq='M'))

print(df.groupby(df.index.to_period('M')).sum().head(2))

         categ
2015-01      1
2015-02      3

But I want to group time series by a custom period, 2 months (bimonthly) for example. Unfortunately to_period('2M') does not work and if I use a TimeGrouper the periods are not fixed, they are relative to the start date.

print(df.groupby(pd.TimeGrouper('2M')).sum().head(2))

            categ
2015-01-31      3
2015-03-31      4

The expected behavior is to have the same fixed period (like a quarter but for two months) whatever the data (Jan-Feb, Mar-Apr, etc.). Does anyone know a solution? Thanks in advance.

           categ
2015-02      3
2015-04      4

Here is the solution I've used for my specific need. If someone has a better answer, let me know.

freq = '2M'
df[freq] = df.index.to_period(freq)
# Adding a month each even month
df[freq] = df[freq] + df[freq].dt.month % 2 * MonthEnd()
print(df.groupby(freq).sum().head(2))

         categ
2M            
2015-02      5
2015-04      2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM