Pandas Grouper – Decades

  • A+
Category:Languages

I have a dataframe of daily observations from 01-01-1973 to 12-31-2014.

Have been using Pandas Grouper and everything has worked fine for each frequency until now: I want to group them by decade 70s, 80s, 90s, etc.

I tried to do it as

import pandas as pd df.groupby(pd.Grouper(freq = '10Y')).mean() 

However, this groups them in 73-83, 83-93, etc.


pd.cut also works to specify a regular frequency with a specified start year.

import pandas as pd df                  date  val 0 1970-01-01 00:01:18    1 1 1979-12-31 18:01:01   12 2 1980-01-01 00:00:00    2 3 1989-01-01 00:00:00    3 4 2014-05-06 00:00:00    4  df.groupby(pd.cut(df.date, pd.date_range('1970', '2020', freq='10YS'), right=False)).mean() #                          val #date                          #[1970-01-01, 1980-01-01)  6.5 #[1980-01-01, 1990-01-01)  2.5 #[1990-01-01, 2000-01-01)  NaN #[2000-01-01, 2010-01-01)  NaN #[2010-01-01, 2020-01-01)  4.0 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: