Home > Software engineering >  Subtracting quarters from a pandas Period
Subtracting quarters from a pandas Period

Time:03-21

I have the following pandas Period

p = pd.Period('2021Q4')

This represents a date 2021-12-31. Is it possible to subtract quarters from this period, i.e., subtract 1, 2, 3 or 4 quarters from this to get pd.Period('2021Q3'), pd.Period('2021Q2'), pd.Period('2021Q1') and pd.Period('2020Q4'), respectively?

I tried the following, but it gave me a IncompatibleFrequency error:

pd.Period('2021Q4')-pd.DateOffset(years=1)

CodePudding user response:

If subtract integers it subtract quarters, because quarter periods:

print (pd.Period('2021Q4')-1)
2021Q3

print (pd.Period('2021Q4')-2)
2021Q2
  • Related