1

This is my current code

from datetime import datetime as dt
df['Age'] = datetime.datetime.now()-pd.to_datetime(df[['Day','Month','Year']])

but my results are

0    3380 days 10:37:40.303097
1    3512 days 10:37:40.303097
2    3131 days 10:37:40.303097
3    3739 days 10:37:40.303097
4    4550 days 10:37:40.303097
5    3204 days 10:37:40.303097
6    3813 days 10:37:40.303097
7    5819 days 10:37:40.303097
8    4532 days 10:37:40.303097
9    2444 days 10:37:40.303097
10   2664 days 10:37:40.303097
11   5527 days 10:37:40.303097
12   3706 days 10:37:40.303097
Name: Age, dtype: timedelta64[ns]

How can I change the above to years?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Yvonne
  • 77
  • 1
  • 7
  • 1
    Does this answer your question? [How to convert Python datetime dates to decimal/float years](https://stackoverflow.com/questions/6451655/how-to-convert-python-datetime-dates-to-decimal-float-years) – Nazim Kerimbekov Jan 13 '20 at 10:42
  • `df['Age'] = df['Age'].apply(lambda x: x.year)` is this what you need – Phung Duy Phong Jan 13 '20 at 10:42
  • Does this answer your question? [Python timedelta in years](https://stackoverflow.com/questions/765797/python-timedelta-in-years) – Dennis Meissel Jan 13 '20 at 10:54
  • I tried "df['Age'] = df['Age'].apply(lambda x: x.year)" and get this error instead "'Timedelta' object has no attribute 'year'" – Yvonne Jan 15 '20 at 02:16

2 Answers2

0

I can't seem to format Timedelta as year - that said, you can use a decent approximation with days - 1 year = 365.25 days

df['Age'] = df['Age'].dt.days #turn timedelta into int number of days
df['Age'] = df['Age']/365.25

Afterwords you can use Round if there's too many decimals for you.

This only counts days. If you want more precision, you can even use seconds with 31556952 seconds in 1 year:

df['Seconds'] = df.Age.dt.days*24*3600+df.Age.dt.seconds #turn timedelta into int number of seconds
df['Years'] = df.Seconds/31556952

Basically your Timedelta is in many units: days, hours, minutes, seconds. You want to transfer that into one unit: float fraction of years. One way to do this is to convert into just one of the available units and then convert into years. Hence:

x = pd.Timedelta('3706 days 10:37:40.303097')
x.seconds
>>38260
x.days*24*3600+x.seconds
>>320236660 #Number of seconds in the Timedelta
(x.days*24*3600+x.seconds)/31556952 #Divided by the number of seconds in a year
>>10.14789577903468
Jim Eisenberg
  • 1,490
  • 1
  • 9
  • 17
  • Using the code, i get 'Age' as this value 9 days 06:10:02.372658 How do I round it? I get an error and also the unit is days instead of years – Yvonne Jan 14 '20 at 02:41
0

It may be too late, but this should also work and I think might be easier:

df['Age'] = df['Age'].astype('<m8[Y]')
rifaaQ
  • 33
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '22 at 00:50