home / github / issues

Menu
  • Search all tables
  • GraphQL API

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 array

savefile = "/path/to/file/BugXarray.nc" arr.to_netcdf(savefile)

Load array

arr_loaded = xr.open_dataset(savefile)

Show time-coordinate on arr and arr_loaded

print(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_timedelta

timedelta = np.arange(200,206,1)/100 timedelta = pd.to_timedelta(timedelta,unit="s")

Show time and timedelta

print(time.values)

Output: [2000000000 2010000000 2020000000 2030000000 2040000000 2050000000]

print(timedelta.values)

Output: [2000000000 2009999999 2020000000 2029999999 2040000000 2049999999]

```

Problem description

Opening 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 Output

In the example above arr_loaded.time.values should equal arr.time.values!

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.2 (default, Dec 29 2018, 06:19:36) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 4.15.0-45-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: de_DE.UTF-8 LOCALE: de_DE.UTF-8 libhdf5: 1.10.4 libnetcdf: 4.6.1 xarray: 0.11.3 pandas: 0.24.1 numpy: 1.15.4 scipy: 1.2.1 netCDF4: 1.4.2 pydap: None h5netcdf: None h5py: 2.9.0 Nio: None zarr: None cftime: 1.0.3.4 PseudonetCDF: None rasterio: None cfgrib: None iris: None bottleneck: 1.2.1 cyordereddict: None dask: 1.1.1 distributed: 1.25.3 matplotlib: 3.0.2 cartopy: None seaborn: 0.9.0 setuptools: 40.8.0 pip: 19.0.1 conda: 4.6.4 pytest: 4.2.1 IPython: 7.2.0 sphinx: 1.8.4
{
    "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

Links from other tables

  • 0 rows from issues_id in issues_labels
  • 1 row from issue in issue_comments
Powered by Datasette · Queries took 0.64ms · About: xarray-datasette