Skip to content

Comments

support timedelta in Q_.init#1978

Open
andrewgsavage wants to merge 12 commits intohgrecco:masterfrom
andrewgsavage:infer_time
Open

support timedelta in Q_.init#1978
andrewgsavage wants to merge 12 commits intohgrecco:masterfrom
andrewgsavage:infer_time

Conversation

@andrewgsavage
Copy link
Collaborator

Copy link
Contributor

@keewis keewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a couple of comments, mostly regarding timedelta64 and where to put the code handling it.

I would also like to have a way to convert a quantity with dimension [time] to a timedelta64, but that can be its own PR (the API is not quite clear to me: should to special-case units of timedelta64[us], for example?).

Comment on lines 231 to 232
if is_timedelta(value) or is_timedelta_array(value):
inst._magnitude = to_seconds(value)
Copy link
Contributor

@keewis keewis May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to special-case numpy.timedelta64, probably in the numpy facet? That way we could roundtrip exactly

pint/compat.py Outdated
if isinstance(obj, datetime.timedelta):
return obj.total_seconds()
elif isinstance(obj, np_timedelta64) or obj.dtype == np_timedelta64:
return obj.astype(float)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not going to work in general: timedelta64 has its own units, which can range from days (or years?) to nanoseconds, and astype(float) is the equivalent of magnitude. I don't have any advice on how to extract that, though, other than parsing the string repr of the dtype.

@jakirkham
Copy link

It looks like this is ready for another review

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 31, 2024

CodSpeed Performance Report

Merging #1978 will not alter performance

Comparing andrewgsavage:infer_time (92e70a1) with master (6e0d038)

Summary

✅ 448 untouched benchmarks

@andrewgsavage
Copy link
Collaborator Author

poke @keewis

@andrewgsavage
Copy link
Collaborator Author

I would also like to have a way to convert a quantity with dimension [time] to a timedelta64, but that can be its own PR (the API is not quite clear to me: should to special-case units of timedelta64[us], for example?).

I think to do we should make time into a nonmultiplactive unit like temperature, so there is then a delta_time unit
the new c++ library does this for all units with two different classes, a point and a vector
#2120

Copy link
Contributor

@keewis keewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for disappearing for quite a while.

Looks good to me in general, but I have two comments.

pint/compat.py Outdated
Comment on lines 115 to 116
if isinstance(obj, ndarray) and obj.dtype.type == np_timedelta64:
return True
Copy link
Contributor

@keewis keewis Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should rely on is_duck_array_type? That would already guarantee the existence of dtype. Also, this will return None if the condition evaluates to False:

Suggested change
if isinstance(obj, ndarray) and obj.dtype.type == np_timedelta64:
return True
return is_duck_array_type(obj) and obj.dtype.type == np_timedelta64


inst = SharedRegistryObject().__new__(cls)

if is_timedelta(value) or is_timedelta_array(value):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels somewhat weird to have the plain facet deal with timedelta arrays. Should that part be in the numpy facet?

Then again, I admit I don't fully grasp the concept of facets.

andrewgsavage and others added 2 commits August 25, 2025 20:14
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

Convert timedelta to quantity

3 participants