I am wondering how to calculate the number of quarters between two dates - these two dates are the end date of a quarter but they're just different in year.
2014-12-31
and 2017-09-30
CodePudding user response:
Convert values to quarter periods, subtract and for integers use attribute n
:
d1 = pd.Timestamp('2017-09-30')
d2 = pd.Timestamp('2014-12-31')
a = (pd.Period(d1, 'q') - pd.Period(d2, 'q')).n
print (a)
11
If need working with Periods in same year use replace
:
a = (pd.Period(d2, 'q') - pd.Period(d1.replace(year=d2.year), 'q')).n
print (a)
1
CodePudding user response:
You question is unclear, but mathematically, the number of quarters couls be calculated using:
date1 = '2014-12-31'
date2 = '2017-09-30'
d1 = pd.to_datetime(date1)
d2 = pd.to_datetime(date2)
out = (d2.year-d1.year)*4 (d2.quarter-d1.quarter)
output: 11