We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
for a dataframe with a detailed timestamp coordinate (2022-03-11T13:50:50.551314000)
2022-03-11T13:50:50.551314000
da.sel(time="2022-03-11") works but da.drop_sel(time="2022-03-11") results in a KeyError
da.sel(time="2022-03-11")
KeyError
i expect drop_sel() to work with the same inputs as sel()
drop_sel()
sel()
import numpy as np import xarray as xr times = np.array(['2022-03-03T13:50:49.548072000', '2022-03-11T13:50:50.551314000', '2022-03-12T13:44:35.547024000', '2022-03-13T12:18:48.015182000'], dtype='datetime64[ns]') da = xr.DataArray(np.arange(4), dims="time", coords={"time": times}) da.drop_sel(time='2022-03-11')
A workaround is to use the exact datestring:
da.drop_sel(time='2022-03-11T13:50:50.551314000')
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[4], line 1 ----> 1 da.drop_sel(time='2022-03-11') File ~/.local/envs/xvec/lib/python3.11/site-packages/xarray/core/dataarray.py:3105, in DataArray.drop_sel(self, labels, errors, **labels_kwargs) 3102 if labels_kwargs or isinstance(labels, dict): 3103 labels = either_dict_or_kwargs(labels, labels_kwargs, "drop") -> 3105 ds = self._to_temp_dataset().drop_sel(labels, errors=errors) 3106 return self._from_temp_dataset(ds) File ~/.local/envs/xvec/lib/python3.11/site-packages/xarray/core/dataset.py:5292, in Dataset.drop_sel(self, labels, errors, **labels_kwargs) 5290 except KeyError: 5291 raise ValueError(f"dimension {dim!r} does not have coordinate labels") -> 5292 new_index = index.drop(labels_for_dim, errors=errors) 5293 ds = ds.loc[{dim: new_index}] 5294 return ds File ~/.local/envs/xvec/lib/python3.11/site-packages/pandas/core/indexes/base.py:6934, in Index.drop(self, labels, errors) 6932 if mask.any(): 6933 if errors != "ignore": -> 6934 raise KeyError(f"{list(labels[mask])} not found in axis") 6935 indexer = indexer[~mask] 6936 return self.delete(indexer) KeyError: "['2022-03-11'] not found in axis"
related to #6605 (comment)
(xarray: 2023.3.0)
commit: None python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0] python-bits: 64 OS: Linux OS-release: 5.4.231-137.341.amzn2.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.9.1
xarray: 2023.3.0 pandas: 1.5.3 numpy: 1.24.2 scipy: 1.10.1 netCDF4: 1.6.3 pydap: None h5netcdf: 1.1.0 h5py: 3.8.0 Nio: None zarr: None cftime: 1.6.2 nc_time_axis: None PseudoNetCDF: None rasterio: 1.3.6 cfgrib: None iris: None bottleneck: None dask: None distributed: None matplotlib: 3.7.1 cartopy: None seaborn: None numbagg: None fsspec: None cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 67.6.0 pip: 23.0.1 conda: None pytest: None mypy: None IPython: 8.11.0 sphinx: None
The text was updated successfully, but these errors were encountered:
Thanks we definitely want everything that works for sel to work for drop_sel.
sel
drop_sel
xarray/xarray/core/dataset.py
Line 5292 in 4448828
index.get_indexer(labels_for_dim) will get you the correct indices to drop. Though probably there's another method that could work.
index.get_indexer(labels_for_dim)
cc @benbovy
Sorry, something went wrong.
No branches or pull requests
What happened?
for a dataframe with a detailed timestamp coordinate (
2022-03-11T13:50:50.551314000
)da.sel(time="2022-03-11")
works but da.drop_sel(time="2022-03-11") results in aKeyError
What did you expect to happen?
i expect
drop_sel()
to work with the same inputs assel()
Minimal Complete Verifiable Example
A workaround is to use the exact datestring:
MVCE confirmation
Relevant log output
Anything else we need to know?
related to #6605 (comment)
Environment
(xarray: 2023.3.0)
INSTALLED VERSIONS
commit: None
python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.231-137.341.amzn2.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1
xarray: 2023.3.0
pandas: 1.5.3
numpy: 1.24.2
scipy: 1.10.1
netCDF4: 1.6.3
pydap: None
h5netcdf: 1.1.0
h5py: 3.8.0
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.3.6
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.7.1
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 67.6.0
pip: 23.0.1
conda: None
pytest: None
mypy: None
IPython: 8.11.0
sphinx: None
The text was updated successfully, but these errors were encountered: