ENH: timedelta64 and datetime64 inherit from np.timebase#10701
ENH: timedelta64 and datetime64 inherit from np.timebase#10701shoyer wants to merge 6 commits intonumpy:mainfrom
Conversation
|
What is this |
|
Apparently something about my local dev environment is stale -- this actually builds successfully in CI! @mhvk I was wondering the same thing. It does look like TimeInteger originally served this purpose, but the same is a poor choice -- it seems wrong to call either datetime64 or timedelta64 integer of any kind. See also @mwiebe commit where he removed TimeInteger in favor of making timedelta inherit from integer: de71993 Any thoughts on the name |
|
Good that there are no build problems! On About the name, I'd be somewhat keen to reserve |
|
I like |
|
What's the advantage of having a common base class at all? They're fundamentally different, incommensurable types. Maybe they should both just be directly under |
I'm not strongly opposed to this. The main advantage is easy categorization, e.g., def isna(array):
if issubclass(array.dtype.type, np.floating):
return np.isnan(array)
elif issubclass(array.dtype.type, np.timebase):
return np.isnat(array)
else:
return np.zeros_like(array, dtype=bool)But I agree that with only two types it's pretty marginal. NumPy does this sort of thing itself in a few places, e.g., in |
|
Also not sure how useful it is, though the two do share the unit mechanism; if this were a python class, it would live in a common base class. Anyway, I guess there are three options of what they should inherit from:
Here, for option (2), I don't think we need to inherit also from I'm mildly in favour of inheriting from a common class, and very mildly prefer option (2), as it uses what now is an empty, confusing slot in the C api. (Indeed, is there any inconvenience associated with changing the numpy C api? If so, would it be a fourth option to rename But most important would seem to be to break the direct inheritance of |
|
Enhancements should have a release note. |
|
Closing, I think the experiment was useful but the two classes do not share enough to have a common ancestor. |
This is attempt to fix #10685 by making both
np.timedelta64andnp.datetime64inherit from a common base class, which I have tentatively callednp.timeunit(which is turn inherits fromnp.generic).Note that this currently doesn't work.
PyTimeunitArrType_Typeends up defined after thePyDatetimeArrType_TypeandPyTimedeltaArrType_Typetypes for which it's supposed to be the base class in the autogenerated__multiarray_api.cand__multiarray_api.h, so the compiler fails with errors about undeclared identifiers. I think this may be because I added it to the end of NumPy C API.If anyone has suggestions for how to make this work, I'd love to hear them. This is my first time attempting to muck with NumPy's C API.