Skip to content

timedelta64[Y] on p.49 is no longer supported in pandas 2? What alternatives are available? #20

@ka2bashi

Description

@ka2bashi

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?

# 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions