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 like10060 days
,
can be converted to years? Or better yet, just converted to an integer representation for years?
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.
This workaround gets you closer.
round((df["Accident Date"] - df["Iw Date Of Birth"]).dt.days / 365, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With