home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 607678694

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
607678694 MDU6SXNzdWU2MDc2Nzg2OTQ= 4010 Issue indexing by xarray's own time values + offset 2405019 closed 0     2 2020-04-27T16:20:34Z 2020-04-28T11:03:06Z 2020-04-28T08:20:16Z CONTRIBUTOR      

I'm struggling to work out how to index by a xarray time value + an offset (either created using np.timedelta64 or datetime.timedelta). I read through https://github.com/pydata/xarray/issues/1240 and https://github.com/pydata/xarray/issues/1240 because they appear related, but I'm not sure how to correctly achieve this.

MCVE Code Sample

```python import xarray as xr import numpy as np import datetime as dt

now = dt.datetime.now() dt_array = xr.DataArray( range(10), dims=('time', ), coords=dict(time=[now + dt.timedelta(seconds=i) for i in range(10)]) )

this works

dt_array.loc[dt_array.time.min():dt_array.time.max()].count() == 10

this fails, only the first value is returned (adding

the time delta appears to have no effect)

dt_array.loc[dt_array.time.min():dt_array.time.min() + np.timedelta64(seconds=4)].count() == 4

this fails, an exception is raised when trying to add

a datetime.timedelta to the xarray value

dt_array.loc[dt_array.time.min():dt_array.time.max() + dt.timedelta(seconds=4)].count() == 4

also fails, I got the impression from issue #1240

that .loc[...] should work for indexing too, but just to double-check

dt_array.sel(time=slice(dt_array.time.min(), dt_array.time.min() + np.timedelta64(seconds=4))).count() == 4

fails, showing that adding a time increment has no effect

dt_array.time.min() + np.timedelta64(seconds=10) != dt_array.time.min() ```

Expected Output

Where I am indexing by the minimum time plus a np.timedelta64 offset of 4 seconds I would expect a DataArray of length 4 to be return. It would be nice if it was possible to add an increment with a native datetime.timedelta object.

Problem Description

I can't work out how to correctly add an increment to a time value in an xarray DataArray. It would be nice if one of the above approaches worked. Or maybe if I'm missing something obvious I could add an example to the documentation on datetime-indexing?

Versions

Output of <tt>xr.show_versions()</tt> INSTALLED VERSIONS ------------------ commit: None python: 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 3.10.0-957.27.2.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8 libhdf5: 1.10.1 libnetcdf: 4.5.0 xarray: 0.15.1 pandas: 0.25.3 numpy: 1.15.4 scipy: 1.1.0 netCDF4: 1.4.0 pydap: None h5netcdf: 0.7.4 h5py: 2.10.0 Nio: None zarr: None cftime: 1.0.2.1 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: 2.2.0 bottleneck: None dask: 0.20.0 distributed: 1.24.0 matplotlib: 2.2.3 cartopy: 0.16.0 seaborn: 0.9.0 numbagg: None setuptools: 46.1.3 pip: 10.0.1 conda: None pytest: 5.3.2 IPython: 7.1.1 sphinx: None
{
    "url": "https://api.github.com/repos/pydata/xarray/issues/4010/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
  • 2 rows from issue in issue_comments
Powered by Datasette · Queries took 0.609ms · About: xarray-datasette