Zarathustra
Zarathustra

Reputation: 145

Convert from year-date to year-quarter in Pandas

I have a column of dates in the format '2000-01', and I want to convert them to '2000q1' accordingly. My question is similar to this post, but I'm not sure how the frequency function is being used there. I've written this code which works, but it's not robust and certainly not efficient:

periods = ['2000-01', '2000-02', '2000-03', '2000-04', '2000-05', '2000-06']
lst = []
for quarter in periods:
    year, month = quarter.split('-')[0], quarter.split('-')[1]
    q1, q2, q3, q4 = ['01', '02', '03'], ['04', '05', '06'], ['07', '08', '09'], ['10', '11', '12']
    if month in q1:
        month = 'q1'
    if month in q2:
        month = 'q2'
    if month in q3:
        month = 'q3'
    if month in q4:
        month = 'q4'
    lst.append(year+month)

What's the best way to do this? Cheers :)

Upvotes: 0

Views: 248

Answers (2)

jezrael
jezrael

Reputation: 863611

Use PeriodIndex:

per = pd.PeriodIndex(periods, freq='Q')
print (per)
PeriodIndex(['2000Q1', '2000Q1', '2000Q1', '2000Q2', '2000Q2', '2000Q2'],
             dtype='period[Q-DEC]', freq='Q-DEC')

And if need lowercase q add PeriodIndex.strftime:

per = pd.PeriodIndex(periods, freq='Q').strftime('%Yq%q')
print (per)
Index(['2000q1', '2000q1', '2000q1', '2000q2', '2000q2', '2000q2'], dtype='object')

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150815

You can use to_periods:

periods = ['2000-01', '2000-02', '2000-03', '2000-04', '2000-05', '2000-06']

s = pd.to_datetime(periods, format='%Y-%m').to_period('Q')

Output:

PeriodIndex(['2000Q1', '2000Q1', '2000Q1', '2000Q2', '2000Q2', '2000Q2'], dtype='period[Q-DEC]', freq='Q-DEC')

Upvotes: 1

Related Questions