Can the following code be considered as an alternative to the above code?
# Calculate a year in seconds
seconds_in_a_year = 60 * 60 * 24 * 365
# convert timedelta to seconds and then to years
scientists['age_years'] = scientists['age_days'].dt.total_seconds() / seconds_in_a_year
print(scientists)
Please let us know if there is a better way.
Thanks.
The following code appears on p.49 in 2nd edition.
scientists['age_years'] = (scientists['age_days'].astype('timedelta64[Y]'))
When this is executed in pandas 2.0.3(Python 3.11.5), the following error is output.
Cannot convert from timedelta64[ns] to timedelta64[Y]. Supported resolutions are 's', 'ms', 'us', 'ns
It seems that timedelta64[Y] is no longer supported in pandas 2.0.0 released in April. This seems to be the cause of the error. https://pandas.pydata.org/docs/dev/whatsnew/v2.0.0.html#construction-with-datetime64-or-timedelta64-dtype-with-unsupported-resolution
Can the following code be considered as an alternative to the above code?
Please let us know if there is a better way. Thanks.