Skip to content

Timedelta64 data cannot be round-tripped to netCDF files without a warning #10099

Open
@shoyer

Description

@shoyer

What is your issue?

We added a future warning about not decoding time units to timedelta64 in #9966 (cc @spencerkclark, @kmuehlbauer).

Unfortunately, this warning is raised by default when reading timedelta64 serialized data to disk. This makes it much harder to use this dtype (which is quite useful for storing the "lead time" dimension in weather forecasts), and means that if we ever do finalize this deprecation warning it will break a lot of users.

I would love to see special handling of timedelta64 data, similar to what I described here: #1621 (comment). In particular, we could write a dtype='timedelta64' attribute (possibly also with a specified precision) when writing a dataset to disk, which could be interpreted as np.timedelta64 data when reading the data with Xarray. This would allow us to at least ensure that datasets with timedelta64 data that are written to Zarr/netCDF now will always be able to be read faithfullly in the future.

To reproduce:

import xarray
import numpy as np

deltas = np.array([1, 2, 3], dtype='timedelta64[D]').astype('timedelta64[ns]')
ds = xarray.Dataset({'lead_time': deltas})
xarray.open_dataset(ds.to_netcdf())

This issues:
FutureWarning: In a future version of xarray decode_timedelta will default to False rather than None. To silence this warning, set decode_timedelta to True, False, or a 'CFTimedeltaCoder' instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions