Home > Enterprise >  Difference between two year-quarter in quarter
Difference between two year-quarter in quarter

Time:12-07

I have a date column. I want to get difference between the fiscal quarter-year of each date with the maximum fiscal quarter-year. This is the way I did:

date = pd.Series(['2019-01-01', '2019-03-01', '2019-07-01', '2020-03-01'])
date = pd.to_datetime(date)
year_quarter = pd.PeriodIndex(date, freq='Q-JAN')
max_year_quarter = year_quarter.max()
print(year_quarter)
print(max_year_quarter)

output:

year_quarter
PeriodIndex(['2019Q4', '2020Q1', '2020Q2', '2021Q1'], dtype='period[Q-JAN]', freq='Q-JAN')
max_year_quarter
Period('2021Q1', 'Q-JAN')

If I run max_year_quarter - max_year_quarter, the output would be:

Index([<5 * QuarterEnds: startingMonth=1>, <4 * QuarterEnds: startingMonth=1>,
       <3 * QuarterEnds: startingMonth=1>, <0 * QuarterEnds: startingMonth=1>],
      dtype='object')

while the expected output is:

[5, 4, 3, 0]

which is the number of quarters between each date and max_year_quarter. I appreciate any help.

CodePudding user response:

This would result in the number of quarters:

Difference = max_year_quarter - year_quarter
Difference_in_Quarter = (max_year_quarter - year_quarter).map(lambda x: x.n)

output:

0    5
1    4
2    3
3    0
dtype: int64

CodePudding user response:

Use Period.ordinal for integer repr and subtract by converting values of PeriodIndex to integers:

Difference_in_Quarter = max_year_quarter.ordinal - year_quarter.astype(int)
print (Difference_in_Quarter)
Int64Index([5, 4, 3, 0], dtype='int64')
  • Related