entercaspa
entercaspa

Reputation: 704

pandas convert date (quarters) to datetime object

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

Upvotes: 6

Views: 6975

Answers (1)

user2285236
user2285236

Reputation:

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.

Upvotes: 12

Related Questions