Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Post a Comment for "Pandas Convert Date (quarters) To Datetime Object"