Conversation
keewis
left a comment
There was a problem hiding this comment.
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?).
pint/facets/plain/quantity.py
Outdated
| if is_timedelta(value) or is_timedelta_array(value): | ||
| inst._magnitude = to_seconds(value) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
It looks like this is ready for another review |
CodSpeed Performance ReportMerging #1978 will not alter performanceComparing Summary
|
|
poke @keewis |
I think to do we should make time into a nonmultiplactive unit like temperature, so there is then a delta_time unit |
keewis
left a comment
There was a problem hiding this comment.
sorry for disappearing for quite a while.
Looks good to me in general, but I have two comments.
pint/compat.py
Outdated
| if isinstance(obj, ndarray) and obj.dtype.type == np_timedelta64: | ||
| return True |
There was a problem hiding this comment.
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:
| if isinstance(obj, ndarray) and obj.dtype.type == np_timedelta64: | |
| return True | |
| return is_duck_array_type(obj) and obj.dtype.type == np_timedelta64 |
pint/facets/plain/quantity.py
Outdated
|
|
||
| inst = SharedRegistryObject().__new__(cls) | ||
|
|
||
| if is_timedelta(value) or is_timedelta_array(value): |
There was a problem hiding this comment.
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.
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
pre-commit run --all-fileswith no errors