10000 Merge pull request #13563 from charris/backport-13332 · numpy/numpy@a8ebc14 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8ebc14

Browse files
authored
Merge pull request #13563 from charris/backport-13332
BUG: Always return views from structured_to_unstructured when possible
2 parents 921578d + c3aff7e commit a8ebc14

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

numpy/lib/recfunctions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'):
972972

973973
# next cast to a packed format with all fields converted to new dtype
974974
packed_fields = np.dtype({'names': names,
975-
'formats': [(out_dtype, c) for c in counts]})
975+
'formats': [(out_dtype, dt.shape) for dt in dts]})
976976
arr = arr.astype(packed_fields, copy=copy, casting=casting)
977977

978978
# finally is it safe to view the packed fields as the unstructured type
@@ -1065,7 +1065,7 @@ def unstructured_to_structured(arr, dtype=None, names=None, align=False,
10651065

10661066
# first view as a packed structured array of one dtype
10671067
packed_fields = np.dtype({'names': names,
1068-
'formats': [(arr.dtype, c) for c in counts]})
1068+
'formats': [(arr.dtype, dt.shape) for dt in dts]})
10691069
arr = np.ascontiguousarray(arr).view(packed_fields)
10701070

10711071
# next cast to an unpacked but flattened format with varied dtypes

numpy/lib/tests/test_recfunctions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ def test_structured_to_unstructured(self):
243243
assert_(dd.base is d)
244244
assert_(ddd.base is d)
245245

246+
# including uniform fields with subarrays unpacked
247+
d = np.array([(1, [2, 3], [[ 4, 5], [ 6, 7]]),
248+
(8, [9, 10], [[11, 12], [13, 14]])],
249+
dtype=[('x0', 'i4'), ('x1', ('i4', 2)), ('x2', ('i4', (2, 2)))])
250+
dd = structured_to_unstructured(d)
251+
ddd = unstructured_to_structured(dd, d.dtype)
252+
assert_(dd.base is d)
253+
assert_(ddd.base is d)
254+
246255
# test that nested fields with identical names don't break anything
247256
point = np.dtype([('x', int), ('y', int)])
248257
triangle = np.dtype([('a', point), ('b', point), ('c', point)])

0 commit comments

Comments
 (0)
0