issues: 415192339
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
415192339 | MDU6SXNzdWU0MTUxOTIzMzk= | 2790 | Bug in xarray.open_dataset with variables/coordinates of dtype 'timedelta64[ns]' | 48060979 | closed | 0 | 1 | 2019-02-27T15:48:14Z | 2022-04-09T02:17:56Z | 2022-04-09T02:17:56Z | NONE | Code Sample, a copy-pastable example if possible```python import xarray as xr import pandas as pd Create array, coordinate time's dtype is timedelta64[ns]time = pd.timedelta_range(f"{2.0}s",f"{2.05}s",freq="10ms",name="time") data = range(len(time)) arr = xr.DataArray(data=psi,coords={"time":time},dims="time",name="psi") Save arraysavefile = "/path/to/file/BugXarray.nc" arr.to_netcdf(savefile) Load arrayarr_loaded = xr.open_dataset(savefile) Show time-coordinate on arr and arr_loadedprint(arr.time.values) Output: [2000000000 2010000000 2020000000 2030000000 2040000000 2050000000]print(arr_loaded.time.values) Output: [2000000000 2009999999 2020000000 2029999999 2040000000 2049999999]Same problem with pandas to_timedeltatimedelta = np.arange(200,206,1)/100 timedelta = pd.to_timedelta(timedelta,unit="s") Show time and timedeltaprint(time.values) Output: [2000000000 2010000000 2020000000 2030000000 2040000000 2050000000]print(timedelta.values) Output: [2000000000 2009999999 2020000000 2029999999 2040000000 2049999999]``` Problem descriptionOpening a netcdf-file that contains variables/coordinates with a dtype that is supposed to be 'timedelta64[ns]' might cause errors due to a loss in precision. I realized that the pandas-function pandas.to_timedelta shows the same misbehavior, though I don't know if xarray.open_dataset uses that function internally. Expected OutputIn the example above arr_loaded.time.values should equal arr.time.values! Output of
|
{ "url": "https://api.github.com/repos/pydata/xarray/issues/2790/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |