You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importxarrayasxrimportnumpyasnpimportdatetimeasdtnow=dt.datetime.now()
dt_array=xr.DataArray(
range(10), dims=('time', ),
coords=dict(time=[now+dt.timedelta(seconds=i) foriinrange(10)])
)
# this worksdt_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 valuedt_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-checkdt_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 effectdt_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?
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 !
I'm struggling to work out how to index by a xarray time value + an offset (either created using
np.timedelta64
ordatetime.timedelta
). I read through #1240 and #1240 because they appear related, but I'm not sure how to correctly achieve this.MCVE Code Sample
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 nativedatetime.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.0xarray: 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
The text was updated successfully, but these errors were encountered: