diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-19 15:24:47 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-19 15:24:47 +0000 |
commit | 5172ae3f46a2da8935e643c7ee6a2086fca3956c (patch) | |
tree | 2ec874eaea07cca772965d8a72a24d7b61cc1c30 | |
parent | b884d998f468f89704dae39ef4d1e0b2bc71a9f3 (diff) | |
download | numpy-5172ae3f46a2da8935e643c7ee6a2086fca3956c.tar.gz |
Fix up rec.array when dtype and formats are both None for cases that support it. Alter dtype=object parsing so that tuples and lists are recognized as sequences.
-rw-r--r-- | numpy/core/records.py | 21 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 17 | ||||
-rw-r--r-- | numpy/core/tests/test_records.py | 16 |
3 files changed, 42 insertions, 12 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index 44f8df8e3..554788253 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -460,35 +460,44 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, raise ValueError("Must define formats (or dtype) if object is "\ "None, string, or an open file") + kwds = {} if dtype is not None: dtype = sb.dtype(dtype) - else: + elif formats is not None: dtype = format_parser(formats, names, titles, aligned, byteorder)._descr + else: + kwds = {'formats': formats, + 'names' : names, + 'titles' : titles, + 'aligned' : aligned, + 'byteorder' : byteorder + } if obj is None: if shape is None: raise ValueError("Must define a shape if obj is None") return recarray(shape, dtype, buf=obj, offset=offset, strides=strides) elif isinstance(obj, str): - return fromstring(obj, dtype, shape=shape, offset=offset) + return fromstring(obj, dtype, shape=shape, offset=offset, **kwds) elif isinstance(obj, (list, tuple)): if isinstance(obj[0], sb.ndarray): - return fromarrays(obj, dtype=dtype, shape=shape) + return fromarrays(obj, dtype=dtype, shape=shape, **kwds) else: - return fromrecords(obj, dtype=dtype, shape=shape) + return fromrecords(obj, dtype=dtype, shape=shape, **kwds) elif isinstance(obj, recarray): new = obj.copy() - new.dtype = dtype + if dtype is not None: + new.dtype = dtype return new elif isinstance(obj, file): return fromfile(obj, dtype=dtype, shape=shape, offset=offset) elif isinstance(obj, sb.ndarray): - if (obj.dtype != dtype): + if dtype is not None and (obj.dtype != dtype): obj.dtype = dtype res = obj.view(recarray) if issubclass(res.dtype.type, nt.void): diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index c20bc4fa4..e0dab85c2 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -6913,10 +6913,13 @@ object_depth_and_dimension(PyObject *s, int max, intp *dims) { intp *newdims, *test_dims; int nd, test_nd; - int i; + int i, islist; intp size; + PyObject *obj; - if (!PyList_Check(s) || ((size=PyList_GET_SIZE(s)) == 0)) + islist = PyList_Check(s); + if (!(islist || PyTuple_Check(s)) || + ((size = PySequence_Size(s)) == 0)) return 0; if (max < 2) { if (max < 1) return 0; @@ -6925,11 +6928,13 @@ object_depth_and_dimension(PyObject *s, int max, intp *dims) } newdims = PyDimMem_NEW(2*(max-1)); test_dims = newdims + (max-1); - nd = object_depth_and_dimension(PyList_GET_ITEM(s, 0), - max-1, newdims); + if (islist) obj = PyList_GET_ITEM(s, 0); + else obj = PyTuple_GET_ITEM(s, 0); + nd = object_depth_and_dimension(obj, max-1, newdims); for (i=1; i<size; i++) { - test_nd = object_depth_and_dimension(PyList_GET_ITEM(s, i), - max-1, test_dims); + if (islist) obj = PyList_GET_ITEM(s, 0); + else obj = PyTuple_GET_ITEM(s, 0); + test_nd = object_depth_and_dimension(obj, max-1, test_dims); if ((nd != test_nd) || (!PyArray_CompareLists(newdims, test_dims, nd))) { nd = 0; diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index b431201b5..8a813253e 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -54,5 +54,21 @@ class test_fromrecords(NumpyTestCase): assert(mine.data1[i]==0.0) assert(mine.data2[i]==0.0) + def check_recarray_from_names(self): + ra = rec.array([ + (1, 'abc', 3.7000002861022949, 0), + (2, 'xy', 6.6999998092651367, 1), + (0, ' ', 0.40000000596046448, 0)], + names='c1, c2, c3, c4') + pa = rec.fromrecords([ + (1, 'abc', 3.7000002861022949, 0), + (2, 'xy', 6.6999998092651367, 1), + (0, ' ', 0.40000000596046448, 0)], + names='c1, c2, c3, c4') + assert ra.dtype == pa.dtype + assert ra.shape == pa.shape + for k in xrange(len(ra)): + assert ra[k].item() == pa[k].item() + if __name__ == "__main__": NumpyTest().run() |