API: Forbid Q<->m safe casting#14718
Conversation
a322678 to
ce2c865
Compare
ce2c865 to
63efb2d
Compare
seberg
left a comment
There was a problem hiding this comment.
Looks good to me (lets see if anything else needs fixing up). As a note for others: This is the minimal fix, since it solves the biggest/new issue while modifying only the strangest part of casting timedelta to ints.
Casting timedelta64 to ints could be disabled more generally at some point. Although that general cast might be best to synchronize to any thoughts about removing the timedelta64 without a unit (base).
63efb2d to
edcd37a
Compare
edcd37a to
6d19421
Compare
|
I just realized this does add a small inconsistency, since we also disable |
|
I pushed a commit to make same kind casting possible I do not quite love this, but I am not sure if there is much of an option if we want consistency. I am also a bit scared that there may be some corner case missing somewhere... |
That is, uint64 <-> timedelta64. Allowing this casting was weird, because Q<->q is forbidden, yet q and m are both int64. This fixes numpygh-12927
|
@seberg: I rebased your commit on top of my local changes to add some more tests. I think your change looks reasonable, but I'd be tempted to pursue it as a separate PR. |
4496add to
ac757b5
Compare
|
Oh, nice, you added some tests. I think I am about ±0 on defining the "same kind" casting. It did seem like a slight inconsistency to not allow it, since by definition same kind casting should be allowed to demote the integer before attempting to "safe" cast to the timedelta. I am OK with living with that inconsistency or splitting it out though. |
|
I think this would be fine to put in - if nothing else, |
tylerjereddy
left a comment
There was a problem hiding this comment.
In the recent review of my PR you indicate
Also a little worried that issubclass(np.timedelta64, numbers.Number) appears to still be true.
I don't see a test enforcing that this is no longer the case? Is that just a separate concern then?
This does look a lot less invasive than my hard blocking of all integer -> m8 conversions. Actually, I thought that was motivated by concerns about unit sloppiness, but maybe that discussion evolved in my absence (I'm 700 emails behind on NumPy now, I think :/).
Overall, looks like you and Sebastian are happier with this & you haven't modified any old unit tests, which is always nice for NumPy.
So I think I'm +1, though I'd say at least good to ping @jbrockmendel on whether this will empower the favorable refactoring inside pandas that was discussed in the context of the original/more invasive change. Perhaps a step in that direction, anyway?
| # issue gh-12927 | ||
| # this only applies to in-place floordiv //=, because the output type | ||
| # promotes to float which does not fit | ||
| a = np.array([1, 2], np.int64) |
There was a problem hiding this comment.
Any reason you changed the original regression test, based on the case originally reported in the issue? It looks like it should have no effect, but then why bother to change it from the original report /my PR?
Perhaps a larger array is more likely to detect problems, I suppose.
There was a problem hiding this comment.
I think I wanted to avoid any weird special cases caused by scalar casting rules. I suppose I should have tested both.
| # Cannot cast safely from unsigned integer of the same size, which | ||
| # could overflow | ||
| assert_(np.can_cast('u8', 'm8', casting='same_kind')) | ||
| assert_(not np.can_cast('u8', 'm8', casting='safe')) |
There was a problem hiding this comment.
Yes, these tests are very clean/nice. Seems pretty clear that this is type-size, not value-size.
I don't know the numpy code well enough to infer from the diff how this will effect downstream behavior. Two candidates come to mind. Will it affect |
|
It might affect |
|
All this affects the behavior of outside the existing tests is code like d = np.datetime64('now')
np.add(d, np.uint64([1, 2]), casting='safe')
td = np.timedelta64(1, 'D')
np.add(td, np.uint64([1, 2]), casting='safe')that is:
I doubt this will impact anything |
|
Should we go forward with this, I think I am in favor, although it is now pretty close to 1.18 as a small downside (although the real test is usually the rc release in any case). |
|
@seberg please check the release notes reflect the second commit. |
|
Since this fixes a regression, I think we should go ahead with it. |
|
OK, I think we can just squash-merge. The release notes are fine, without the second commit, |
|
Thanks @eric-wieser, @seberg |
A lighter-weight alternative to gh-13061, fixes #12927