hoof_hearted
hoof_hearted

Reputation: 675

Convert String to Date [With Year and Quarter]

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.

Upvotes: 9

Views: 14276

Answers (2)

jezrael
jezrael

Reputation: 862741

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

Upvotes: 9

Julien Marrec
Julien Marrec

Reputation: 11895

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)

Upvotes: 4

Related Questions