diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-04-14 15:49:15 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-05-01 08:55:34 -0700 |
commit | e6a7e05753f8a5f56de9653bfecd85a22f53d7c6 (patch) | |
tree | 818f4d1771b3081ab59c49b67651233bdc5ad0bf /numpy/lib/recfunctions.py | |
parent | b5895be146cdc3063ffa9ca8ae27b5bcf7992719 (diff) | |
download | numpy-e6a7e05753f8a5f56de9653bfecd85a22f53d7c6.tar.gz |
BUG: Always return views from structured_to_unstructured when possible
Also applies to unstructured_to_structured
While producing correct resutls, the test added in this commit would previously make an unecessary copy, causing the assertion to fail.
The cause was `astype` was being asked to convert from a subarray of shape `(x, y)` to one of `(x*y,)`, which it cannot do without making a copy.
This changes the approach used to skip the step of flattening subarrays to 1d
Diffstat (limited to 'numpy/lib/recfunctions.py')
-rw-r--r-- | numpy/lib/recfunctions.py | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index bf588a490..d412281ff 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -972,7 +972,7 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): # next cast to a packed format with all fields converted to new dtype packed_fields = np.dtype({'names': names, - 'formats': [(out_dtype, c) for c in counts]}) + 'formats': [(out_dtype, dt.shape) for dt in dts]}) arr = arr.astype(packed_fields, copy=copy, casting=casting) # 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, # first view as a packed structured array of one dtype packed_fields = np.dtype({'names': names, - 'formats': [(arr.dtype, c) for c in counts]}) + 'formats': [(arr.dtype, dt.shape) for dt in dts]}) arr = np.ascontiguousarray(arr).view(packed_fields) # next cast to an unpacked but flattened format with varied dtypes |