Repository navigation
MAINT: casting changes for int -> m8 - #13061
tylerjereddy wants to merge 1 commit into
Conversation
* prevent int64 //= uint64 from succeeding (a recent regression) by preventing the safe cast from all integer types and bool to timedelta64 * corresponding unit test additions / adjustments
3d932d8 to
b078513
Compare
|
|
||
|
|
||
| @pytest.mark.parametrize("int_typecode", | ||
| list(np.typecodes['AllInteger'] + '?')) |
There was a problem hiding this comment.
I've added bool in here now as well
| # that dot() supports | ||
| dtypes = [np.dtype(code) for code in np.typecodes['All'] | ||
| if code not in 'USVM'] | ||
| if code not in 'USVMm'] |
There was a problem hiding this comment.
a reviewer should probably confirm that the need for this with the casting changes makes sense; I think the need for this only snuck in after I prohibited bool from casting to timedelta64
There was a problem hiding this comment.
Calling dot on timedeltas is nonsense, so this change seems fine
|
I've performed the following build / test sequences on
Traceback details: Details |
|
The initial reaction on the pandas side seems very positive (cross-linked above). The casting effect on built-in |
|
To summarize one thing that surprised me a little from making changes in pandas: arr = np.array([0, 1, 2], dtype='m8[s]')
np.sum(arr) # works
arr[0] + arr[1] + arr[2] # works
# The following doesn't work--CPython must
# try to work with ints and cast back?
sum(arr)
# numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' input 0 from dtype('int64') to dtype('<m8[s]') with casting rule 'same_kind'Not sure there's much we can do about built-in |
|
Buultin sum adds 0 as a default. |
|
Thanks Eric, I'll mention that in the pandas PR--I guess it isn't a big deal. |
|
Note that the built-in sum should be avoided anyway, it doesn't produce the right dtype when the array is empty. |
|
Compensating changes merged on the |
|
I'd like this in... As If a fast sum is required, use an |
|
@tylerjereddy is this still a going concern? If/when this happens, pandas can rip out some ugly arithmetic shims that implement it there. |
|
Probably still is a concern. @jbrockmendel and @hameerabbasi would you want to rush this? If not too much, I will tag this with 1.18 and you can nudge us to merge it soon after the 1.17.x branching? Just a thought, I am starting to worry a bit about making 1.17 too large (and it is getting close in any case). I suppose we are missing a bit of review here, but it seems fairly straight forward and far along on first glance. |
|
Definitely don't rush on pandas' account. Even once it's merged it'll be a few release cycles before we can rely on it. |
|
Yes, 1.18.x is fine. 🙂 |
|
@tylerjereddy anything I can do to help make this a reality? |
|
@jbrockmendel Maybe @eric-wieser or @seberg can chime in on if this is still desired & an appropriate time for the change & also if we need warnings / deprecations in this case. I'm a little less active on NumPy than SciPy lately but can probably (slowly) respond to reviewer requests. |
|
Let's try to put this in. @tylerjereddy could you fix the conflict and add a release note? @eric-wieser would you like to take one last look? |
|
|
||
| _npy_can_cast_safely_table[NPY_STRING][NPY_UNICODE] = 1; | ||
| _npy_can_cast_safely_table[NPY_BOOL][NPY_TIMEDELTA] = 1; | ||
| _npy_can_cast_safely_table[NPY_BOOL][NPY_TIMEDELTA] = 0; |
There was a problem hiding this comment.
I think this should go on line 3916 instead, as
if (i != NPY_DATETIME && i != NPY_TIMEDELTA) {
/*
* Bool -> <Anything> except datetime (since
* it would assume implicit units)
*/
_npy_can_cast_safely_table[NPY_BOOL][i] = 1;
}
| a = np.arange('1969-05', '1970-05', 2, dtype='M8') | ||
| assert_equal(a.dtype, np.dtype('M8[M]')) | ||
| assert_equal(a, | ||
| np.datetime64('1969-05') + np.arange(12, step=2)) |
There was a problem hiding this comment.
It would be great if we could keep this test somehow... Trying to work out how.
There was a problem hiding this comment.
If gh-14714 goes in, adding dtype='m8' to the arange call would do the trick.
Without it, you need to change 12 to 0, 12 as well.
| offsets = np.array([0, 0, 1, 1, 2, 3, 5, 10, 20], dtype='m8[D]') | ||
| bins = np.array([0, 2, 7, 20], dtype='m8[D]') | ||
| dates = begin + offsets | ||
| date_bins = begin + bins |
There was a problem hiding this comment.
This doesn't look right - I think you should leave what you touched unchanged, and it should read
dates = begin + offsets.astype('m8[D]')
date_bins = begin + bins.astype('m8[D]')
| # NOTE: produces DeprecationWarning with recent casting adjustment | ||
| # blocking int64-> m8 | ||
| #assert_equal((d_edge - begin).astype(int), i_edge) | ||
| #assert_equal(t_edge.astype(int), i_edge) |
There was a problem hiding this comment.
With the change I suggest above you should be able to leave this in
| if not issubclass(stype, Number) or issubclass(stype, bool): | ||
| continue | ||
| s = stype(5) | ||
| if stype == np.timedelta64: |
There was a problem hiding this comment.
Can we just not register timedelta64 as a Number? Or is that too hard?
eric-wieser
left a comment
There was a problem hiding this comment.
Not a huge fan of weakening the tests to pull this off. Also a little worried that issubclass(np.timedelta64, numbers.Number) appears to still be true.
|
@tylerjereddy do you want help updating this from the reviews? |
|
Opst, I think this was superceded by gh-14718 |
|
Thanks Tyler, the other one is in, so closing this one! |
Alternative to #12940 that Fixes #12927.
This approach is a bit more invasive in terms of likely effect on downstream libraries, but was preferred by core developers in the linked discussions above.
This will likely still need: