Convert String to Date [With Year and Quarter]

10,582

Solution 1

You can use split, then cast column year to int and if necessary add Q to column q:

df = pd.DataFrame({'date':['2015Q1','2015Q2']})
print (df)
     date
0  2015Q1
1  2015Q2

df[['year','q']] = df.date.str.split('Q', expand=True)
df.year = df.year.astype(int)
df.q = 'Q' + df.q
print (df)
     date  year   q
0  2015Q1  2015  Q1
1  2015Q2  2015  Q2

Also you can use Period:

df['date'] = pd.to_datetime(df.date).dt.to_period('Q')

df['year'] = df['date'].dt.year
df['quarter'] = df['date'].dt.quarter

print (df)
    date  year  quarter
0 2015Q1  2015        1
1 2015Q2  2015        2

Solution 2

You could also construct a datetimeIndex and call year and quarter on it.

df.index = pd.to_datetime(df.date)
df['year'] = df.index.year
df['quarter'] = df.index.quarter

              date  year  quarter
date                             
2015-01-01  2015Q1  2015        1
2015-04-01  2015Q2  2015        2

Note that you don't even need a dedicated column for year and quarter if you have a datetimeIndex, you could do a groupby like this for example: df.groupby(df.index.quarter)

Share:
10,582

Related videos on Youtube

hoof_hearted
Author by

hoof_hearted

Updated on October 19, 2022

Comments

  • hoof_hearted
    hoof_hearted over 1 year

    I have a pandas dataframe, where one column contains a string for the year and quarter in the following format:

    2015Q1
    

    My Question: ​How do I convert this into two datetime columns, one for the year and one for the quarter.

    • Zeugma
      Zeugma over 7 years
      For what it's worth, pandas has a period type. pd.Period('2015Q1') gives you an object you call call .year and .quarter. And do period related calculations
  • hoof_hearted
    hoof_hearted over 7 years
    Perfect Solution - Many Thanks!
  • Julien Marrec
    Julien Marrec over 7 years
    So you actually don't want a datetime?