2

I just cant find the answer and I know pandas eats problems like this for desert.

I have a datetime index and want to know its length, in years:

idx=pd.date_range('2011-07-03', '2015-07-10')

expected output:

4.0191 years   (4 years and 7 days)

If I do: idx[0]-idx[-1] I get an output in days, but id like it in years

Sorry: couldn't locate on panads docs

jpp
  • 159,742
  • 34
  • 281
  • 339
Junaid Mohammad
  • 457
  • 1
  • 6
  • 18
  • 2
    Is `4.0191` years really accurate? Years can vary in length, I'm not sure I see the usefulness of that value. – roganjosh Aug 30 '18 at 10:59
  • I have a return value. I need to annualise it by dividing by the number of years. my index is my datetime index. I just wanted its lenth in years to be my denominator. 4.0191 years may not be the exact answer. I wrote it because I manually calculated it was 4 years, plus the 3rd july to 10th july days – Junaid Mohammad Aug 30 '18 at 11:02

3 Answers3

4

You can convert timedelta to days and then divide by 365.25 if is not necessary 100% accuracy:

idx=pd.date_range('2011-07-03', '2015-07-10')

print ((idx[-1]-idx[0]).days / 365.25)

4.0191649555099245

But if need years with days:

from dateutil.relativedelta import relativedelta

r = relativedelta(idx[-1], idx[0])
print('{} years {} days'.format(r.years, r.days))
4 years 7 days
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

Using ptp and np.timedelta64:

>>> idx.to_series().ptp() / np.timedelta64(1, 'Y')
4.019247486259129

If you want it to be more specific (down to the days) and get the Timedelta object, just don't divide:

>>> idx.to_series().ptp()
Timedelta('1468 days 00:00:00')

If the date_range is always sorted, then using to_series with ptp adds unnecessary complexity, and you can use part of @jezrael's answer

>>> (idx[-1] - idx[0]) / np.timedelta64(1, 'Y')
4.019247486259129
user3483203
  • 50,081
  • 9
  • 65
  • 94
1

You can also use relativedelta as follows:

from dateutil.relativedelta import relativedelta

relativedelta(idx[0], idx[-1]).years

Make sure that the later date is the first argument.

Hodossy Szabolcs
  • 1,598
  • 3
  • 18
  • 34