Skip to content

Issue indexing by xarray's own time values + offset #4010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leifdenby opened this issue Apr 27, 2020 · 2 comments
Closed

Issue indexing by xarray's own time values + offset #4010

leifdenby opened this issue Apr 27, 2020 · 2 comments

Comments

@leifdenby
Copy link
Contributor

leifdenby commented Apr 27, 2020

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 #1240 and #1240 because they appear related, but I'm not sure how to correctly achieve this.

MCVE Code Sample

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 xr.show_versions() 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

@keewis
Copy link
Collaborator

keewis commented Apr 27, 2020

take a look at the result of np.timedelta64(seconds=4):

In [2]: np.timedelta64(seconds=4)
Out[2]: numpy.timedelta64(0)

In [3]: np.timedelta64(dt.timedelta(seconds=4))
Out[3]: numpy.timedelta64(4000000,'us')

In [4]: np.timedelta64(4, 's').astype("timedelta64[us]")
Out[4]: numpy.timedelta64(4000000,'us')

so you need to also specify the desired unit or cast a dt.timedelta object. I'm not sure why timedelta64 ignores the seconds kwarg, though.

@leifdenby
Copy link
Contributor Author

leifdenby commented Apr 28, 2020

Ah! It's an issue with how I am using np.timedelta64 then. I (stupidly) assumed that np.timedelta64 has the same call signature as datetime.timedelta. It appears that np.timedelta64 silently ignores any kwargs 😕

Anyway, I won't be making that mistake again. Thank you @keewis !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants