pythonpandastslib

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


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


Solution

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

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