How do you convert a column containing year & quarter in a str format as '1947q1' to date format column where both year and quarter are considered?
问题 **Year_qtr GDP ADJ_GDP** 2 1947q1 243.1 1934.5 3 1947q2 246.3 1932.3 4 1948q3 250.1 1930.3 5 1949q4 260.3 1960.7 Tried parse() from dateutil package but didnt wwork. Result dataframe should have 'Year_qtr' column as date values instead of object. 回答1: pandas already can do this out of the box! you can cast to datetime right away: import pandas as pd df = pd.DataFrame({'Year_qtr': ['1947q1', '1947q2', '1948q3', '1949q4']}) df['datetime'] = pd.to_datetime(df['Year_qtr']) # df # Year_qtr