Is there any way to extract mm-yyyy (or even quarter-yyyy) information from a datetime variable?
My datetime column is df['event'] which is a dd-mm-yyyy. I know I can extract mm and year from my variable, but is there a way I can extract the two combined?
I can resample my data to monthly frequency, but then I would only get the mean of my variable of interests, while I want to keep all my observations.
You can use Series.dt.to_period for month or quarter periods:
df = pd.DataFrame({'event':['01-01-2015','02-05-2015','01-08-2016','01-11-2015']})
df['event'] = pd.to_datetime(df['event'], dayfirst=True)
df['months'] = df['event'].dt.to_period('M')
df['quarters'] = df['event'].dt.to_period('Q')
print (df)
event months quarters
0 2015-01-01 2015-01 2015Q1
1 2015-05-02 2015-05 2015Q2
2 2016-08-01 2016-08 2016Q3
3 2015-11-01 2015-11 2015Q4
If need strings in custom formats add strftime:
df['months'] = df['event'].dt.strftime('%m-%Y')
df['quarters'] = df['event'].dt.to_period('Q').dt.strftime('%q-%Y')
print (df)
event months quarters
0 2015-01-01 01-2015 1-2015
1 2015-05-02 05-2015 2-2015
2 2016-08-01 08-2016 3-2016
3 2015-11-01 11-2015 4-2015
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With