Support encoding times with units finer than the data resolution
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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:
- 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
OutOfBoundsDatetimeorOutOfBoundsTimedelta, also when a float dtype was requested. Should a float dtype give floats instead? - 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 withKeyError: 'nanoseconds'. The first version raised the error after thetryblock, the same way #11907 does now. - 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.
- Ngôn ngữ chính
- Python
- Star
- 4.2k
- Fork
- 1.5k
- Merge trung bình
- 2 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 74
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của pydata/xarray
-
xarray removes axis grid from pcolormeshCó thể đã có người làm @Sealdot đã nhận hôm nay. Đang mởbug needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`DataTree.map_over_datasets` behavior can be obscure sometimesCó thể đã có người làm @mohansree14 đã nhận 24 ngày trước. Đang mởneeds triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
pydata/xarray#11813 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identicalCó thể đã có người làm @SohamXYZDev đã nhận 29 ngày trước. Đang mởbug needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
pydata/xarray#11777 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Có thể đã có người làm @d-chambers đã nhận 60 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
pydata/xarray#11598 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
DataTree constructor error message says dict even though dict is not acceptedCó thể đã có người làm @aryansk đã nhận 100 ngày trước. Đang mởbug contrib-good-first-issue topic-DataTree topic-error reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
pydata/xarray#11514 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của pydata/xarray
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
abetlen/llama-cpp-python#2424 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug IronPython Python 3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
pyrevitlabs/pyRevit#4184 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CLAW (Agents & Orchestration)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày