Support encoding times with units finer than the data resolution

Đang mở
#11,920 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
numpy, pandas, python
Lĩnh vực
backend, data

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:

  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.

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

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pydata/xarray

Tất cả issue của pydata/xarray

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.