-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Cannot use xarrays own times for indexing #1240
Comments
Yes, this is annoying. We already have some work arounds for this in xarray -- see |
I get the same error when trying to do: |
Hi, this is still the case for version 0.10.0.
fails, but doing the same thing on a pandas dataframe works just fine:
I'd really appreciate see this working on a DataArray. |
@ulijh - you should be able to use the time index: In [11]: arr.loc[arr.indexes['time'][2]:arr.indexes['time'][5]]
Out[11]:
<xarray.DataArray (time: 4, space: 3)>
array([[ 0.422991, 0.513276, 0.762432],
[ 0.111123, 0.371109, 0.697921],
[ 0.029415, 0.215116, 0.451697],
[ 0.670181, 0.855551, 0.319134]])
Coordinates:
* time (time) datetime64[ns] 2000-01-03 2000-01-04 2000-01-05 2000-01-06
* space (space) <U2 'IA' 'IL' 'IN' |
@jhamman - thanks, this should be usefull... |
Sorry for letting this one linger for so long... I added the "bug" tag so we don't forget about it for the next release. |
This was closed and was solved for slicing, but not for element indexing: import xarray as xr
import numpy as np
da = xr.DataArray([0, 1], dims=("time",), coords={"time": np.array([0, 1], dtype="M8[s]")})
da.sel(time=da.coords["time"][0]) results in
using xarray 0.15.2.dev64+g2542a63f (latest master). I think it would be desirable that it works in both cases. Should we reopen this issue or should I open a new? |
I fixed my conda environment now (something was wrong as I appeared to have two xarray installations in parallel). I still get the
Oops, by "already have" you meant it's already been reported, I thought you meant it had already been fixed. All clear then. |
I need to get the first Δt from the start of my dataset, i.e.
ds.sel(start_time, start_time + timedelta)
. However, due to pandas usingM8[ns]
but datetime.datetime not supporting this, the index gets converted to anint
and indexing fails. Inspection tells me that by the time the index reachespandas
it is already an int. This is ultimately due to the numpy problem thattimedelta64(0, 'ns').item()
is anint
, but it would be very nice ifxarray
had a workaround so that we can use indexing such as shown below.The text was updated successfully, but these errors were encountered: