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
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.
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