Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'TimedeltaProperties' object has no attribute 'years' in Pandas

In Pandas, why does a TimedeltaProperties object have no attribute 'years'?

After all, the datetime object has this property.

It seems like a very natural thing for an object that is concerned with time to have. Especially if it already has an hours, seconds, etc attribute.

Is there a workaround so that my column, which is full of values like
10060 days,
can be converted to years? Or better yet, just converted to an integer representation for years?

like image 592
tumultous_rooster Avatar asked Jun 20 '15 04:06

tumultous_rooster


2 Answers

TimedeltaProperties does not have year or month attributes because according to TimedeltaProperties source code . It is -

Accessor object for datetimelike properties of the Series values.

But , months or years have no constant definition.

1 month can take on different different number of days, based on the month itself, like January -> 31 days , April -> 30 days , etc.

1 month can take on different values based on the year as well (in case of February month) , if the year is 2004 , February has 29 days , if the year is 2003 February has 28 days, etc.

Same is the case with years , it can take on different values based on which exact year it is, for example - if the year is 2003 , it has 365 days, if the year is 2004 it has 366 days.

Hence, an requirement like - Convert 10060 days to years is not accurate, which years?

Like previously stated, the accurate amount of years those no. of days correspond to depend on the actual years those days represent.

like image 70
Anand S Kumar Avatar answered Oct 23 '22 11:10

Anand S Kumar


This workaround gets you closer.

round((df["Accident Date"] - df["Iw Date Of Birth"]).dt.days / 365, 1)

like image 6
pabz Avatar answered Oct 23 '22 11:10

pabz