[Numpy-discussion] Renaming a field of an object array
Brent Pedersen
bpederse at gmail.com
Wed Feb 4 18:25:17 EST 2009
On Wed, Feb 4, 2009 at 2:50 PM, Pierre GM <pgmdevlist at gmail.com> wrote:
> All,
> I'm a tad puzzled by the following behavior (I'm trying to correct a
> bug in genfromtxt):
>
> I'm creating an empty structured ndarray, using np.object as dtype.
>
> >>> a = np.empty(1,dtype=[('',np.object)])
> array([(None,)],
> dtype=[('f0', '|O4')])
>
> Now, I'd like to rename the field:
> >>> a.view([('NAME',np.object)])
> TypeError: Cannot change data-type for object array.
>
> I understand why I can't change the *type* of the field, but not why I
> can't change its name that way. What would be an option that wouldn't
> involve creating a new array ?
> Thx in advance.
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
hi, i was looking at this as well. the code in arrayobject.c doesnt
match the error string. i changed the code to do what the error string
says and seems thing to work.
i think the if-block below it should also use xor (not changed in this
patch), but i'm not a c programmer so i may be missing something
obvious.
svn diff numpy/core/src/arrayobject.c
Index: numpy/core/src/arrayobject.c
===================================================================
--- numpy/core/src/arrayobject.c (revision 6338)
+++ numpy/core/src/arrayobject.c (working copy)
@@ -6506,9 +6506,16 @@
PyErr_SetString(PyExc_TypeError, "invalid data-type for array");
return -1;
}
- if (PyDataType_FLAGCHK(newtype, NPY_ITEM_HASOBJECT) ||
- PyDataType_FLAGCHK(newtype, NPY_ITEM_IS_POINTER) ||
- PyDataType_FLAGCHK(self->descr, NPY_ITEM_HASOBJECT) ||
+ if (PyDataType_FLAGCHK(newtype, NPY_ITEM_HASOBJECT) ^
+ PyDataType_FLAGCHK(self->descr, NPY_ITEM_HASOBJECT)) {
+ PyErr_SetString(PyExc_TypeError, \
+ "Cannot change data-type for object " \
+ "array.");
+ Py_DECREF(newtype);
+ return -1;
+ }
+
+ if (PyDataType_FLAGCHK(newtype, NPY_ITEM_IS_POINTER) ||
PyDataType_FLAGCHK(self->descr, NPY_ITEM_IS_POINTER)) {
PyErr_SetString(PyExc_TypeError, \
"Cannot change data-type for object " \
More information about the NumPy-Discussion
mailing list