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 reason seems to be that old code just copied the points of the object, while the new code goes through PyArray_CopyInto which calls dtype->get_element on each entry.
The get_elemnt of int arrays converts it to python integers in python2.
This causes a test failure in scipy where a int type array now compares false to the np.int32 type array (test_stats.py)
I'm not sure whats the best way to fix that or if it even should be fixed.
Or identical, but maybe even closer to the question, .astype(object) will also convert the elements. If converting them there is right, then doing it everywhere might make sense, too...
I am seriously starting to think the change is good, but we should change the behaviour back by also changing that assignment subtlety. It would be a subtle change, but I really can't see why we should lose type information on object array assignments (won't rule out that it can be useful at times, but I think it is rather unexpected) or casting to object.
I just tried and it creates a couple of test failures, would have to investigate further if that might be an option or the failures are not easy to fix. It would be the change I mentioned above...
OK, so the problem with that above change is probably "just" that the numpy scalar types call into the ufunc/array machinery for their stuff. This leads to an infinite loop:
numpy_scalar + object
-> array(numpy_scalar) + object
-> for item in array(numpy_scalar) + object
-> numpy_scalar + object
Well, I think the "correct" thing would be to do that change I mentioned. Since that requires some rework of the scalars... As far as I understood the problems scipy had with this are fixed or not very substantial. So, while ideally, I think we basically reverse the current object casting behaviour (reverse in the sense of the np.array change), this is probably not as bad as to block 1.9, I guess.
I think the problem here is that object arrays have two distinct (sometimes incompatible) functions: 1) to allow arrays containing arbitrary python objects, and 2) to cast numpy-numeric-types to python-numeric-types. The two functions are conflicting in this issue.
is useful for nested ndarrays of different lengths, or for arrays of special numeric types (eg mpmath's multiprecision objects). 2) is useful for large integers: In order to compute arange(10) + 2**128 numpy casts to Object (converting to python integers) since python integers have arbitrary size.
Here are a few examples that helped me think about what was going on. The current rule seems to be: If the sub-sequences are the same length cast all elements to the closest compatible type (which may be object/python-types if there is any non-numeric element). If the sub-sequences are different length, create an object array but do not cast to object.
However we do it, I think there should be a way of creating object arrays both with and without casting to python types. What should that look like?
One far-out idea is doing arange(10).astype(np.object) could give you an object array where each element is an np.int scalar, while arange(10).astype(np.pytype) would give you an object array of python ints.
I am going to close this. I think that gh-16876 basically half covers this, and its so many years ago that we should not worry about the initial "regression" anymore, I think.
Activity
seberg commentedon Nov 8, 2013
Hmmm, this is not quite clear to me. The old behaviour somewhat seems more sensible to me, however this overlaps:
I.e. the numpy type gets converted to the python type in object assignments due to CopyTo behaviour as if
a.item(0)got called.seberg commentedon Nov 8, 2013
Or identical, but maybe even closer to the question,
.astype(object)will also convert the elements. If converting them there is right, then doing it everywhere might make sense, too...seberg commentedon Dec 6, 2013
I am seriously starting to think the change is good, but we should change the behaviour back by also changing that assignment subtlety. It would be a subtle change, but I really can't see why we should lose type information on object array assignments (won't rule out that it can be useful at times, but I think it is rather unexpected) or casting to object.
charris commentedon Feb 24, 2014
@juliantaylor @seberg What is the status here? Sounds like this should be fixed.
seberg commentedon Feb 27, 2014
One option may be something like this (likely not exactly this):
I just tried and it creates a couple of test failures, would have to investigate further if that might be an option or the failures are not easy to fix. It would be the change I mentioned above...
seberg commentedon Apr 28, 2014
OK, so the problem with that above change is probably "just" that the numpy scalar types call into the ufunc/array machinery for their stuff. This leads to an infinite loop:
kaboom.
charris commentedon May 5, 2014
I'm inclined to leave this be for 1.9. @juliantaylor @seberg Thoughts?
seberg commentedon May 5, 2014
Well, I think the "correct" thing would be to do that change I mentioned. Since that requires some rework of the scalars... As far as I understood the problems scipy had with this are fixed or not very substantial. So, while ideally, I think we basically reverse the current object casting behaviour (reverse in the sense of the
np.arraychange), this is probably not as bad as to block 1.9, I guess.charris commentedon May 6, 2014
@seberg OK, I'll leave this open and remove it from the 1.9 blockers. Do you think it should be a 1.10 blocker? That way it won't get completely lost.
ahaldane commentedon Mar 8, 2015
it seems to me it should return a void scalar. Eg:
Edit: Oh I see that's what it originally did
ahaldane commentedon Mar 8, 2015
I think the solution might be to add a new function
VOID_to_OBJECTinarraytypes.c.src, instead of using@FROMTYPE@_to_OBJECTas used presently.ahaldane commentedon Mar 14, 2015
After examining it a little more:
I think the problem here is that object arrays have two distinct (sometimes incompatible) functions: 1) to allow arrays containing arbitrary python objects, and 2) to cast numpy-numeric-types to python-numeric-types. The two functions are conflicting in this issue.
arange(10) + 2**128numpy casts to Object (converting to python integers) since python integers have arbitrary size.Here are a few examples that helped me think about what was going on. The current rule seems to be: If the sub-sequences are the same length cast all elements to the closest compatible type (which may be object/python-types if there is any non-numeric element). If the sub-sequences are different length, create an object array but do not cast to object.
However we do it, I think there should be a way of creating object arrays both with and without casting to python types. What should that look like?
One far-out idea is doing
arange(10).astype(np.object)could give you an object array where each element is annp.intscalar, whilearange(10).astype(np.pytype)would give you an object array of python ints.charris commentedon Jun 19, 2015
@seberg @ahaldane I'm going to remove this from the 1.10 blockers. Do you think is still need fixing at some point?
njsmith commentedon Jun 19, 2015
It looks to me like a valid bug, but it's not a 1.10 blocker in any sense.
On Jun 18, 2015 8:08 PM, "Charles Harris" [email protected] wrote:
ahaldane commentedon Jun 19, 2015
I agree with @njsmith, it's not urgent
charris commentedon Jan 12, 2016
Bumbing up to 1.12 release, and will continue to do so until someone steps up to fix this.
seberg commentedon Sep 9, 2020
I am going to close this. I think that gh-16876 basically half covers this, and its so many years ago that we should not worry about the initial "regression" anymore, I think.