Home > OS >  How to correctly convert Year-month to Year-quarter
How to correctly convert Year-month to Year-quarter

Time:05-22

I am trying to convert a pandas dataframe containing date in YYYYMM format to YYYYQ format as below

import pandas as pd
dat = pd.DataFrame({'date' : ['200612']})
pd.PeriodIndex(pd.to_datetime(dat.date), freq='Q')

However this generates output as 2012Q2, whereas correct output should be 2006Q4

What is the right way to get correct Quarter?

CodePudding user response:

Explicitly specific the input format:

dat = pd.DataFrame({'date' : ['200612']})
pd.PeriodIndex(pd.to_datetime(dat.date, format='%Y%m'), freq='Q')

Output:

PeriodIndex(['2006Q4'], dtype='period[Q-DEC]', name='date')
  • Related