Support encoding times with units finer than the data resolution
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.5k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 74
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydata/xarray
-
bug needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
`DataTree.map_over_datasets` behavior can be obscure sometimesPossibly taken @mohansree14 claimed this 23 days ago. Openneeds triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pydata/xarray#11813 · 1 comment ·
Maintainers usually reply within 1 day
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identicalPossibly taken @SohamXYZDev claimed this 29 days ago. Openbug needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
pydata/xarray#11777 · 1 comment ·
Maintainers usually reply within 1 day
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Possibly taken @d-chambers claimed this 60 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pydata/xarray#11598 · 3 comments ·
Maintainers usually reply within 1 day
-
DataTree constructor error message says dict even though dict is not acceptedPossibly taken @aryansk claimed this 100 days ago. Openbug contrib-good-first-issue topic-DataTree topic-error reporting
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pydata/xarray#11514 · 2 comments ·
Maintainers usually reply within 1 day
Similar issues
-
tests
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
[Plugin] 群聊记忆 · 人格养成Open
Difficulty 1/5 1-3 hours Newbie friendliness 74/100
-
bug maintainer-managed
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
awslabs/visual-asset-management-system#1759 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100