[NumPy-Tickets] [NumPy] #1977: assignment of scalar subclass of int to a slice of of a recarray field of np.object silently upcasts
NumPy Trac
numpy-tickets@scipy....
Thu Nov 10 19:49:25 CST 2011
#1977: assignment of scalar subclass of int to a slice of of a recarray field of
np.object silently upcasts
---------------------------------------+------------------------------------
Reporter: hughsw | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: Unscheduled
Component: numpy.core | Version: 1.6.0
Keywords: scalar field slice assign |
---------------------------------------+------------------------------------
{{{
# Demonstrates a Numpy bug whereby assigning from a scalar that is a
subclass of
# int into a slice of a recarray field of type np.object will lose the
subclass info
import numpy as np
def numpy_bug():
"""
>>> print 'np.version.version', np.version.version
np.version.version 1.6.1
>>> numpy_bug()
[('foo', '|O8')]
[<class '__main__.MyInt'>] (52,)
[<class '__main__.MyInt'>] (53,)
[<type 'int'>] (100,)
[<type 'int'>] (100,)
"""
# named field of dtype np.object
rec_object_t = np.dtype([('foo', np.object)])
# subclass of int
class MyInt(int): pass
# set the items, using two forms of broadcasting
items = np.empty((4,), dtype=rec_object_t)
# this works
items['foo'][:2] = map(MyInt, xrange(52, 54))
# this fails: it loses the MyInt type, silently casting to int even
though the
# 'foo' field slot is of dtype np.object
items['foo'][2:] = MyInt(100)
print rec_object_t
for item in items: print map(type, item), ' ', item
import doctest
doctest.testmod()
}}}
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1977>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list