pandas convert date (quarters) to datetime object

后端 未结 1 1784
青春惊慌失措
青春惊慌失措 2021-01-19 20:01

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 Dom         


        
相关标签:
1条回答
  • 2021-01-19 20:29

    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.

    0 讨论(0)
提交回复
热议问题