`.sel()` fails on `datetime64[s]` object
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从提供的 NumPy/xarray MVCE 开始,跟踪 DataArray.sel 对字符串切片和 datetime64[s] 值所使用的时间坐标路径。根据所示的 pandas traceback 确认该失败;当 2400–2420 的选择无需 cftime 即可成功,并由回归测试覆盖时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
What happened?
Hi there,
sorry if this might be a duplicate, but I have been browsing the repo without finding anything specific which resemble this.
So, I am exploring to the possibility of calling xarray with CFDatetimeDecoder on time period overshooting pandas threshold year 2262
Running with xarray=2025.9.0
import xarray as xr
files="/ec/res4/scratch/ecme3497/ece4/pic2/output/oifs/pic2_atm_cmip6_1m_24*.nc"
coder = xr.coders.CFDatetimeCoder(time_unit='s')
data = xr.open_mfdataset(files, decode_times=coder)
data.time_counter
which gives me
array(['2400-01-16T12:00:00', '2400-02-15T12:00:00', '2400-03-16T12:00:00',
..., '2489-10-16T12:00:00', '2489-11-16T00:00:00',
'2489-12-16T12:00:00'], shape=(1080,), dtype='datetime64[s]')
so far so good. However
data.sel(time_counter=slice("2400-01-01", "2420-01-01"))
OverflowError Traceback (most recent call last)
File pandas/_libs/tslibs/period.pyx:1169, in pandas._libs.tslibs.period.period_ordinal_to_dt64()
OverflowError: Overflow occurred in npy_datetimestruct_to_datetime
The above exception was the direct cause of the following exception:
OutOfBoundsDatetime Traceback (most recent call last)
Cell In[8], [line 1](vscode-notebook-cell:?execution_count=8&line=1)
----> [1](vscode-notebook-cell:?execution_count=8&line=1) data.sel(time_counter=slice("2400-01-01", "2420-01-01"))
File /ECMWF_kQYjfeo/miniforge/envs/env1/lib/python3.12/site-packages/xarray/core/dataset.py:2974, in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
2906 """Returns a new dataset with each array indexed by tick labels
2907 along the specified dimension(s).
2908
(...) 2971
2972 """
2973 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> [2974](https://vscode-remote+ssh-002dremote-002bhpc-002dlogin.vscode-resource.vscode-cdn.net/ECMWF_kQYjfeo/miniforge/envs/env1/lib/python3.12/site-packages/xarray/core/dataset.py:2974) query_results = map_index_queries(
2975 self, indexers=indexers, method=method, tolerance=tolerance
2976 )
2978 if drop:
2979 no_scalar_variables = {}
...
File pandas/_libs/tslibs/period.pyx:1992, in pandas._libs.tslibs.period._Period.to_timestamp()
File pandas/_libs/tslibs/period.pyx:1172, in pandas._libs.tslibs.period.period_ordinal_to_dt64()
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2400-01-01 00:00:00
I know this can be fixed using cftime, but I though that using datetime64[s] would have fix this issue. In general, I am quite confused on what are the currently supported features of xarray for going beyond the the nanoseconds limitation of pandas. Shall we keep working with cftime? Is there something I am missing? To what extent can we used the Coder if this is the output?
Any help, also suggestion to documentation, is greatly appreciated!
Thanks a lot
EDIT: I can upload some of the data required, but below you can find a reproducible example
What did you expect to happen?
As expected, correct time selection.
Minimal Complete Verifiable Example
import numpy as np
import xarray as xr
# monthly starts from 2389-01 to 2489-12 (inclusi)
months = np.arange("2389-01", "2490-01", dtype="datetime64[M]") # dtype M = month starts
times = months.astype("datetime64[s]") # convert to seconds (YYYY-MM-01T00:00:00)
# creare DataArray di esempio
da = xr.DataArray(np.zeros(times.size), coords={"time": times}, dims=["time"])
da.sel(time=slice("2400-01-01", "2420-01-01"))
I have been able to replicate this also in most recent version of xarray
>>> xr.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.14.0 | packaged by conda-forge | (main, Dec 2 2025, 20:23:19) [Clang 20.1.8 ]
python-bits: 64
OS: Darwin
OS-release: 23.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: ('it_IT', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2025.11.0
pandas: 2.3.3
numpy: 2.3.5
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 25.3
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Environment
xarray>=2025.9.0
- 主要语言
- Python
- 星标
- 4.2k
- 派生
- 1.5k
- 平均合并
- 2 天 20 小时
- 30 天内合并 PR
- 70
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pydata/xarray 的其他 Issue
-
xarray removes axis grid from pcolormesh可能已有人在做 @Sealdot 于 1 天前认领。 未关闭bug needs triage
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
`DataTree.map_over_datasets` behavior can be obscure sometimes可能已有人在做 @mohansree14 于 24 天前认领。 未关闭needs triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identical可能已有人在做 @SohamXYZDev 于 30 天前认领。 未关闭bug needs triage
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 1 天内回复
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)可能已有人在做 @d-chambers 于 61 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
DataTree constructor error message says dict even though dict is not accepted可能已有人在做 @aryansk 于 100 天前认领。 未关闭bug contrib-good-first-issue topic-DataTree topic-error reporting
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
相似的 Issue
-
documentation priority: low size: S status: needs triage
难度 2/5 1-3 小时 新手友好度 74/100
cubrid-lab/sqlalchemy-cubrid#1397 ·
维护者通常 1 天内回复
-
documentation priority: low size: S status: needs triage
难度 2/5 1-3 小时 新手友好度 72/100
cubrid-lab/pycubrid#1307 ·
维护者通常 1 天内回复
-
[Bug]: Installer hides SSL certificate errors as bare URLError, and the suggested fix can't work未关闭bug
难度 2/5 1-3 小时 新手友好度 80/100
ShaftHQ/chaosengine-harness#143 ·
维护者通常 1 天内回复
-
LLMContextNode tool builders fail on nested params like `params:retrieval.max_matches`可能已有人在做 @Shizoqua 今天认领。 未关闭Community
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复
-
security
难度 2/5 1-3 小时 新手友好度 78/100
btclib-org/portanode#985 ·
维护者通常 1 天内回复