3

I have two dates:

a = (pd.to_datetime(20191231, format='%Y%m%d') + pd.offsets.MonthEnd(n=0)).to_period('Q')
b = (pd.Timestamp(str(2020) + '-' + str(4 * 3)) + pd.offsets.MonthEnd(n=0)).to_period('Q')

I calculated their difference:

c = a - b

The type of c is still pandas._libs.tslibs.offsets.QuarterEnd. Is there a way I can convert c to integer (# of quarter difference)?

wwj123
  • 365
  • 2
  • 12

1 Answers1

3

We can use the n attribute from QuarterEnd to get the expected result:

>>> c = a - b
>>> c.n
-4
tlentali
  • 3,407
  • 2
  • 14
  • 21
  • Thank you @tlentali. More of a follow up question: if I have a pandas series of QuarterEnd, would there be a similar way to convert it to a pandas series of integer? I know I can use a for loop to do it but I feel there is better way to do so. – wwj123 Dec 06 '21 at 17:52
  • 1
    I think I can just use apply(lambda) to do it. Thank you @tlentali. – wwj123 Dec 06 '21 at 17:55