Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cc7bc3

Browse files
committed
Merge pull request numpy#6643 from ahaldane/recarray_getitem_returns_recarray
ENH: make recarray.getitem return a recarray
2 parents cdef8d3 + c06726d commit 9cc7bc3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

numpy/core/records.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def __getitem__(self, indx):
502502
# we might also be returning a single element
503503
if isinstance(obj, ndarray):
504504
if obj.dtype.fields:
505+
obj = obj.view(recarray)
505506
if issubclass(obj.dtype.type, nt.void):
506507
return obj.view(dtype=(self.dtype.type, obj.dtype))
507508
return obj

numpy/core/tests/test_records.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ def test_recarray_views(self):
121121
assert_equal(type(rv), np.recarray)
122122
assert_equal(rv.dtype.type, np.record)
123123

124+
#check that getitem also preserves np.recarray and np.record
125+
r = np.rec.array(np.ones(4, dtype=[('a', 'i4'), ('b', 'i4'),
126+
('c', 'i4,i4')]))
127+
assert_equal(r['c'].dtype.type, np.record)
128+
assert_equal(type(r['c']), np.recarray)
129+
assert_equal(r[['a', 'b']].dtype.type, np.record)
130+
assert_equal(type(r[['a', 'b']]), np.recarray)
131+
124132
# check that accessing nested structures keep record type, but
125133
# not for subarrays, non-void structures, non-structured voids
126134
test_dtype = [('a', 'f4,f4'), ('b', 'V8'), ('c', ('f4',2)),

0 commit comments

Comments
 (0)