Skip to content Skip to sidebar Skip to footer

How To Start Time Group On A Specific Date Pandas

I have a df that I have grouped by datetime '12M'. I would like it to be grouped by 12M ending Dec. 31. Currently it is yearly grouping ending Jan. 31. It seems that there must be

Solution 1:

You can use Grouper with freq=A:

Offset aliases.

g=df.groupby([pd.Grouper(level='date',freq='A'),'cand_id','trans_typ'])['amount'].sum()printgdatecand_idtrans_typ2001-12-31  H2HI0211024K25002007-12-31  H8IL2102124K-1000S6TN0021624K20002008-12-31  H2PA1109824K1000H4KS0310524K49664H6KS0114624K2000H6KS0318324K1000H8KS0209024K1000S6TN0021624K25002009-12-31  H0MO0001924K500H8MO0915324K500S0MO0018324K1000S0NY0041024K0S2KY0001224K2000S6OH0016324K-4000S6TN0021624K-2000S6WY0006824K-3500

Solution 2:

Use the closed='left' argument with pd.TimeGrouper.

pd.TimeGrouper('12M', closed='left')

Post a Comment for "How To Start Time Group On A Specific Date Pandas"