diff options
-rw-r--r-- | numpy/lib/recfunctions.py | 12 | ||||
-rw-r--r-- | numpy/lib/tests/test_recfunctions.py | 8 |
2 files changed, 13 insertions, 7 deletions
diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index 2f82d01cc..6e257bb3f 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -32,7 +32,7 @@ __all__ = [ 'join_by', 'merge_arrays', 'rec_append_fields', 'rec_drop_fields', 'rec_join', 'recursive_fill_fields', 'rename_fields', 'repack_fields', 'require_fields', - 'stack_arrays', 'structured_to_unstructured', + 'stack_arrays', 'structured_to_unstructured', 'unstructured_to_structured', ] @@ -977,7 +977,12 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): return arr.view((out_dtype, (sum(counts),))) -def _unstructured_to_structured(arr, dtype=None, names=None, align=False, +def _unstructured_to_structured_dispatcher(arr, dtype=None, names=None, + align=None, copy=None, casting=None): + return (arr,) + +@array_function_dispatch(_unstructured_to_structured_dispatcher) +def unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe'): """ Converts and n-D unstructured array into an (n-1)-D structured array. @@ -1017,6 +1022,7 @@ def _unstructured_to_structured(arr, dtype=None, names=None, align=False, Examples -------- + >>> from numpy.lib import recfunctions as rfn >>> dt = np.dtype([('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) >>> a = np.arange(20).reshape((4,5)) >>> a @@ -1024,7 +1030,7 @@ def _unstructured_to_structured(arr, dtype=None, names=None, align=False, [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) - >>> _unstructured_to_structured(a, dt) + >>> rfn.unstructured_to_structured(a, dt) array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])], dtype=[('a', '<i4'), ('b', [('f0', '<f4'), ('f1', '<u2')]), ('c', '<f4', (2,))]) diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index cb357941c..0126ccaf8 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -10,7 +10,7 @@ from numpy.testing import assert_, assert_raises from numpy.lib.recfunctions import ( drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields, find_duplicates, merge_arrays, append_fields, stack_arrays, join_by, - repack_fields, _unstructured_to_structured, structured_to_unstructured, + repack_fields, unstructured_to_structured, structured_to_unstructured, apply_along_fields, require_fields, assign_fields_by_name) get_fieldspec = np.lib.recfunctions._get_fieldspec get_names = np.lib.recfunctions.get_names @@ -220,7 +220,7 @@ class TestRecFunctions(object): assert_equal(out, np.array([ 1. , 4. , 7. , 10. ])) c = np.arange(20).reshape((4,5)) - out = _unstructured_to_structured(c, a.dtype) + out = unstructured_to_structured(c, a.dtype) want = np.array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), (10, (11., 12), [13., 14.]), @@ -241,7 +241,7 @@ class TestRecFunctions(object): d = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'i4')]) dd = structured_to_unstructured(d) - ddd = _unstructured_to_structured(dd, d.dtype) + ddd = unstructured_to_structured(dd, d.dtype) assert_(dd.base is d) assert_(ddd.base is d) @@ -250,7 +250,7 @@ class TestRecFunctions(object): (8, [9, 10], [[11, 12], [13, 14]])], dtype=[('x0', 'i4'), ('x1', ('i4', 2)), ('x2', ('i4', (2, 2)))]) dd = structured_to_unstructured(d) - ddd = _unstructured_to_structured(dd, d.dtype) + ddd = unstructured_to_structured(dd, d.dtype) assert_(dd.base is d) assert_(ddd.base is d) |