panda timedelta64[ns] to years

Viewed 1350

I need to to tranform the values in a panda dataframe from timedelta64[ns] to years, but I have that measure in days. For business exigenses I need to express tha measure in years. how could I do ?

1 Answers

You can just divide the timedelta with another timedelta with a length of one year*:

In [1]: import pandas as pd 

In [2]: delta = pd.Timedelta('10000000000000000 ns')

In [3]: delta / pd.Timedelta('365 days')
Out[3]: 0.31709791983764585

*How many days in a year?

This really depends on what you are doing with the data. Some examples:

  • 365 days in common calendar year
  • 365.2425 days in Gregorian calendar year
  • 365.25 days Julian astronomical year

Also, some calendar years have 366 days (when a calendar leap occurs)

Related