Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a mm-yyyy column in pandas?

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.

like image 543
Filippo Sebastio Avatar asked Jan 28 '26 09:01

Filippo Sebastio


1 Answers

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
like image 120
jezrael Avatar answered Jan 31 '26 02:01

jezrael