xarray-contrib / xarray-contrib/pint-xarray

interaction with `timedelta64`

Open
#220 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
119
Forks
15
PR merge metrics
No merged PRs in 30d

Description

Arrays with dtype timedelta64 are basically quantities with dimension [time], and they even have explicit units.

It would be really useful to be able to convert between those and pint quantities. The core logic should probably live in pint, but I think we should figure out a way to make the conversion easier:

arr = xr.Dataset(
    coords={
        "delta1": ("time", np.arange(100, dtype="timedelta64[s]")),
        "delta2": ("time", np.arange(100), {"units": "s"})
    }
).pint.quantify()
arr.pint.to({"delta1": "s", "delta2": "timedelta64[s]"})

(not sure if overloading .to would be worth it, but if not I'm sure we can come up with something better).

Similarly, I'd like to be able to infer the frequency of a datetime64 object and get the frequency (if any) as a quantity. It seems this works (found here):

from pandas.tseries.frequencies import to_offset

timedelta = to_offset(xr.infer_freq(time)).delta.to_numpy()

where the timedelta would then be converted to pint using the functionality described above.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the accessor paths behind Dataset(...).pint.quantify() and .pint.to(), then inspect xarray.infer_freq and pandas.tseries.frequencies.to_offset. Done should cover the proposed timedelta64-to-quantity conversion and datetime64 frequency handling, but the issue leaves the API and implementation boundary open.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.