thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

inconsistent comparison of object type arrays (Trac #2117) #5913

Open numpy-gitbot opened 11 years ago

numpy-gitbot commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2117 on 2012-04-26 by atmention:yarikoptic, assigned to unknown.

This issue became more visible since 1.6.x allowed to construct such heterogeneous arrays without specification of dtype=object:

python -c 'import numpy as np; print np.__version__; a = np.array([np.array([0, 1]), np.array(1)]); print a.dtype; print a == a.copy(); print a == np.array([np.array([0, 1]), np.array(1)])'  
1.7.0.dev-3cb783e
object
[ True  True]
False

So -- comparing an object array to itself's copy worked just fine but comparison to identically created another one -- failed.

numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-04-27

Strange indeed. This is going to be fun to figure out. My guess it that the first item is the same object in a and a.copy(), whereas it is a different object for the other array created with the same parameters... and it is so.

In [12]: b = np.array([np.array([0, 1]), np.array(1)], dtype=object)

In [13]: type b[1]
-------> type(b[1])
Out[13]: numpy.ndarray

In [14]: c = b.copy()

In [15]: c[0] is b[0]
Out[15]: True

In [16]: d = np.array([0,1])

In [17]: c[0] is d
Out[17]: False

So the comparison is comparing object pointers rather than calling a comparison routine. It's certainly easier to compare the pointers. I'm not sure if this is a bug or a feature ;) Shouldn't be hard to find where it happens.

numpy-gitbot commented 11 years ago

atmention:charris wrote on 2012-04-27

OTOH

In [37]: a = np.array([[257]*2, 257], dtype=object)

In [38]: b = np.array([[257]*2, 257], dtype=object)

In [39]: a == b
Out[39]: array([ True,  True], dtype=bool)

In [40]: a[0] is b[0]
Out[40]: False

So it looks specific to arrays.