Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas convert date (quarters) to datetime object

Tags:

python

pandas

I have a pandas DataFrame that has a column (Title) that needs to be parsed as a datetime object so I can turn this into a time series.

    Title   Gross Domestic Product: Quarter on Quarter growth: CVM SA %
224 2009 Q3 0.1
225 2009 Q4 0.4
226 2010 Q1 0.5

can anyone point out what would be the best approach to this?

my desired output would be

    Title   Gross Domestic Product: Quarter on Quarter growth: CVM SA %
224 2009-09 0.1
225 2009-12 0.4
226 2010-03 0.5
like image 729
entercaspa Avatar asked Mar 08 '23 15:03

entercaspa


1 Answers

If there is no space between the Year and the Quarter, pandas can parse it so you need to replace the space character:

pd.to_datetime(df['Title'].str.replace(' ', '')) + pd.offsets.QuarterEnd(0)
Out: 
0   2009-09-30
1   2009-12-31
2   2010-03-31
Name: Title, dtype: datetime64[ns]

By default it gives you the start date of the quarter so I added an offset as described here.

like image 70
ayhan Avatar answered Mar 24 '23 21:03

ayhan