Lynn
Lynn

Reputation: 4398

Change quarter format

I have a dataframe, df, where I would like to change the column values of a 4 digit quarter date to a two digit

Data

id    date

aa    Q1.2022
bb    Q2.2025
aa    Q3.2022

Desired

id    date

aa    Q1.22
bb    Q2.25
aa    Q3.22

Doing

df['date'] = pd.PeriodIndex(df['date'], freq='Q').strftime('Q%q.%y')

Note: Pandas version = 1.1.3

Upvotes: 0

Views: 128

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Try via str.split() and str.join():

df['date']=df['date'].str.split('.').str[::-1].str.join('-')
df['date']=pd.PeriodIndex(df['date'],freq='Q').strftime('Q%q.%y')

output of df:

    id  date
0   aa  Q1.22
1   bb  Q2.25
2   aa  Q3.22

Upvotes: 1

Related Questions