Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interaction with timedelta64 #220

Open
keewis opened this issue Jun 14, 2023 · 2 comments
Open

interaction with timedelta64 #220

keewis opened this issue Jun 14, 2023 · 2 comments

Comments

@keewis
Copy link
Collaborator

keewis commented Jun 14, 2023

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.

@lumbric
Copy link

lumbric commented May 6, 2024

I had the same question and posted it here on stackoverflow.

@andrewgsavage
Copy link

The core logic should probably live in pint

Here's a PR for the pint logic
hgrecco/pint#1978

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants