python - convert pandas._libs.tslibs.offsets.QuarterEnd to integer

855 Views Asked by At

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)?

1

There are 1 best solutions below

2
On BEST ANSWER

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

>>> c = a - b
>>> c.n
-4