Support encoding times with units finer than the data resolution

未关闭
#11,920 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
numpy, pandas, python
领域
backend, data

调研方向

Start with encode_cf_datetime in xarray/coding/times.py and trace how datetime and timedelta encoders convert values to the requested unit, including the cftime fallback. Review the handling of overflow and float dtypes, then inspect the existing encoder tests. Done means finer requested units work where representable, overflow behavior is decided and covered, and datetime and timedelta encode/decode round trips remain exact.

由索引模型根据 Issue 内容生成。

描述

Is your feature request related to a problem?

Since #11907, encoding datetime64 or timedelta64 values with units finer than the resolution of the data raises a ValueError. Before that, every value was silently encoded as NaT. @kmuehlbauer asked me to open an issue to discuss how a follow-up PR could support this case.

import numpy as np
from xarray.coding.times import encode_cf_datetime

times = np.array(["2000-01-02T00:00:01"], dtype="datetime64[s]")
encode_cf_datetime(times, "milliseconds since 2000-01-01")
# ValueError: Times can't be serialized with requested units 'milliseconds since 2000-01-01', because these units are finer than the resolution of the data ('seconds'). ...
Describe the solution you'd like

The first version of #11907 (e2f70ea, 819f73f) converted the time deltas to the finer unit with TimedeltaIndex.as_unit before the division. @spencerkclark wrote that this looks like the right approach for a full solution. Units equal to or coarser than the data took the same path as before.

Points that I think need a decision:

  1. Values that do not fit. The cast overflows when the values do not fit into int64 with the finer units, for example times 500 years from the reference date with nanoseconds units. The first version raised OutOfBoundsDatetime or OutOfBoundsTimedelta, also when a float dtype was requested. Should a float dtype give floats instead?
  2. cftime fallback. In the datetime encoder the overflow error is caught by the except (OutOfBoundsDatetime, OverflowError, ValueError) clause and sent to the cftime fallback, which then fails with KeyError: 'nanoseconds'. The first version raised the error after the try block, the same way #11907 does now.
  3. Tests. The first version tested the six unit pairs (seconds to milliseconds, microseconds and nanoseconds, milliseconds to microseconds and nanoseconds, microseconds to nanoseconds) with three dtypes, for both encoders. Would you prefer a smaller set?
Describe alternatives you've considered

Keep the error from #11907 and leave it to the user to cast the data to a finer resolution before writing.

Additional context

With the conversion applied on top of #11906, I checked an encode and decode round trip for four data resolutions, seven units from days to nanoseconds, three dtypes, and both datetimes and timedeltas, each with one NaT. All 168 combinations came back exact.

Empty and all-NaT arrays already encode today and would not need to change.

I'm happy to open the PR once the approach is agreed.

主要语言
Python
星标
4.2k
派生
1.5k
平均合并
2 天 21 小时
30 天内合并 PR
74

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pydata/xarray 的其他 Issue

查看 pydata/xarray 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。