You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The //= operation between an int64-array and a uint64-array fails in 1.16.0 but it succeeds in 1.16.1. It seems the result changed unintentionally (see below). I'm not sure which is the desired behavior.
1.16.0
Traceback (most recent call last):
File "a.py", line 5, in <module>
a //= b
TypeError: ufunc 'floor_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
% git log --oneline | head -n 3
68d4a8397 Revert "Merge pull request #12308 from tylerjereddy/timedelta64_floordiv"
0e97167ee Revert "Merge pull request #12683 from tylerjereddy/timedelta64_divmod"
c009e1b98 Merge pull request #12894 from eric-wieser/distutils-broken-import
1.17.0.dev0+c009e1b
ok
1.17.0.dev0+0e97167
ok
1.17.0.dev0+68d4a83
Traceback (most recent call last):
File "a.py", line 5, in <module>
a //= b
TypeError: ufunc 'floor_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
Python version information:
3.6.7 (default, Nov 7 2018, 12:26:42)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)]
Commenting out TypeDescription('m', FullTypeDescr, 'mm', 'q'), restores the original behavior, but obviously reverts the enhancement to m8. I spent a few hours debugging through linear_search_type_resolver and ufunc_loop_matches, and it looks like types[i] == NPY_OBJECT becomes true for the original case & not for the new case. I can sort of patch to restore the TypeError conditionally on input types to the division type resolver, but actually we do have unit tests that fail if I do that it seems.
I'm guessing integer-like types can kind of follow timedelta64 through control flow in surprising ways because of their similarity.
What is clear is that the behavior that changed silently as far as unit tests are considered needs to be tested.
Just one other thought here -- how disruptive to end users / large downstream libraries do we think a prohibition on the Q->m + q->m casting would be? If it sneaks through in pandas or somewhere like that I suppose we may have to use a little caution.
I agree with Eric here, 2. seems like a good option (making it an unsafe cast). Certainly will need a deprecation period (if we get lucky, a short one), I guess only trying will tell how tricky doing that actually is. I suppose the type resolver could be fixed up to "skip" the deprecation for this specific function.
I tried removing the safe cast, the fallout in our testsuit is pretty limited (mostly specific tests and tests for addition subtraction). Also a few tests of true divide in the polynomial, which is probably only tested because timedelta's are also numbers.Number (another point that I am a bit unsure about).
I think we can probably deprecate it in a proper way, but will need to add new ufunc loops (unless they are supposed to deprecated as well) with some care.
The other thing I am wondering about is whether we should disallow "same_kind" casting for out-of-kind casts. Unlike the documentation suggests, the current logic allows many unsafe out of kind casts.
Hmm, I'm torn on this now. The point that "it's sloppy from a unit perspective" isn't really valid - we have a "generic" timedelta type that is deliberately sloppy, and allowing to convert between that an an int64 seems pretty reasonable.
Perhaps we should just disable uint64 <-> timedelta64, in the same way that today we disable uint64 <-> int64?
Activity
eric-wieser commentedon Feb 4, 2019
cc @tylerjereddy
eric-wieser commentedon Feb 4, 2019
@toslunar: So to be clear, your conclusion is that #12308 is the cause of the behavior change?
toslunar commentedon Feb 4, 2019
Yes. My guess is that #12308 is the cause, while I've not located which lines in the PR changed the behavior.
tylerjereddy commentedon Feb 5, 2019
Commenting out
TypeDescription('m', FullTypeDescr, 'mm', 'q'),restores the original behavior, but obviously reverts the enhancement to m8. I spent a few hours debugging throughlinear_search_type_resolverandufunc_loop_matches, and it looks liketypes[i] == NPY_OBJECTbecomes true for the original case & not for the new case. I can sort of patch to restore theTypeErrorconditionally on input types to the division type resolver, but actually we do have unit tests that fail if I do that it seems.I'm guessing integer-like types can kind of follow timedelta64 through control flow in surprising ways because of their similarity.
What is clear is that the behavior that changed silently as far as unit tests are considered needs to be tested.
eric-wieser commentedon Feb 5, 2019
Nice detective work @tylerjereddy!
eric-wieser commentedon Feb 6, 2019
Added some debug prints to the C code - we're hitting the
dtype('<m8'),dtype('<m8')->dtype('int64')loop (mm->q) in this case.I think the rationale is that for
qQ->q, we allowq->mandQ->mconversion. So as a result,mm->qis a matching loop.Some options we should consider:
Q->mcasting, it's lossyQ->morq->mcasting, it's sloppy from a unit perspectivePersonally, i think 2 is the right direction to move in
tylerjereddy commentedon Feb 6, 2019
Just one other thought here -- how disruptive to end users / large downstream libraries do we think a prohibition on the Q->m + q->m casting would be? If it sneaks through in pandas or somewhere like that I suppose we may have to use a little caution.
eric-wieser commentedon Feb 6, 2019
We could start by emitting a warning in that case, and see how disruptive it ends up being.
seberg commentedon Feb 12, 2019
I agree with Eric here, 2. seems like a good option (making it an unsafe cast). Certainly will need a deprecation period (if we get lucky, a short one), I guess only trying will tell how tricky doing that actually is. I suppose the type resolver could be fixed up to "skip" the deprecation for this specific function.
seberg commentedon Feb 14, 2019
Not sure it helps, but if maybe an easier start for someone confused about the casting logic in numpy than digging through the code paths: https://gist.github.com/seberg/925ee5ecb312a467ce822c90adbf5659
seberg commentedon Feb 15, 2019
I tried removing the safe cast, the fallout in our testsuit is pretty limited (mostly specific tests and tests for addition subtraction). Also a few tests of true divide in the polynomial, which is probably only tested because timedelta's are also
numbers.Number(another point that I am a bit unsure about).I think we can probably deprecate it in a proper way, but will need to add new ufunc loops (unless they are supposed to deprecated as well) with some care.
The other thing I am wondering about is whether we should disallow "same_kind" casting for out-of-kind casts. Unlike the documentation suggests, the current logic allows many unsafe out of kind casts.
eric-wieser commentedon Oct 15, 2019
Hmm, I'm torn on this now. The point that "it's sloppy from a unit perspective" isn't really valid - we have a "generic" timedelta type that is deliberately sloppy, and allowing to convert between that an an
int64seems pretty reasonable.Perhaps we should just disable
uint64 <-> timedelta64, in the same way that today we disableuint64 <-> int64?bsipocz commentedon Oct 15, 2019
This currently works on master:
eric-wieser commentedon Oct 15, 2019
Yep - unfortunately, the intent was that this should not work.