diff options
author | Allan Haldane <allan.haldane@gmail.com> | 2018-04-13 14:10:35 -0400 |
---|---|---|
committer | Allan Haldane <allan.haldane@gmail.com> | 2018-04-13 15:18:51 -0400 |
commit | 288667deb24bfb51bb2dbe37ab33f77ec7124fb4 (patch) | |
tree | f3c723c0a3449b6d645b06aa2134a301ac6b9935 | |
parent | eae4808aa49531eabca2554ffc49bdadf81f857d (diff) | |
download | numpy-288667deb24bfb51bb2dbe37ab33f77ec7124fb4.tar.gz |
BUG: for 1.14 back-compat, accept list-of-lists in fromrecords
Fixes #10870
-rw-r--r-- | numpy/core/records.py | 56 | ||||
-rw-r--r-- | numpy/core/tests/test_records.py | 8 |
2 files changed, 40 insertions, 24 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index 612d39322..b3f285584 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -676,32 +676,40 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None, else: descr = format_parser(formats, names, titles, aligned, byteorder)._descr - try: - retval = sb.array(recList, dtype=descr) - except (TypeError, ValueError): - if (shape is None or shape == 0): - shape = len(recList) - if isinstance(shape, (int, long)): - shape = (shape,) - if len(shape) > 1: - raise ValueError("Can only deal with 1-d array.") - _array = recarray(shape, descr) - for k in range(_array.size): - _array[k] = tuple(recList[k]) - # list of lists instead of list of tuples ? - # 2018-02-07, 1.14.1 - warnings.warn( - "fromrecords expected a list of tuples, may have received a list " - "of lists instead. In the future that will raise an error", - FutureWarning, stacklevel=2) - return _array - else: - if shape is not None and retval.shape != shape: - retval.shape = shape + # deprecated back-compat block for numpy 1.14, to be removed in a later + # release. This converts list-of-list input to list-of-tuples in some + # cases, as done in numpy <= 1.13. In the future we will require tuples. + if (isinstance(recList, list) and len(recList) > 0 + and isinstance(recList[0], list) and len(recList[0]) > 0 + and not isinstance(recList[0][0], (list, tuple))): + + try: + memoryview(recList[0][0]) + except: + if (shape is None or shape == 0): + shape = len(recList) + if isinstance(shape, (int, long)): + shape = (shape,) + if len(shape) > 1: + raise ValueError("Can only deal with 1-d array.") + _array = recarray(shape, descr) + for k in range(_array.size): + _array[k] = tuple(recList[k]) + # list of lists instead of list of tuples ? + # 2018-02-07, 1.14.1 + warnings.warn( + "fromrecords expected a list of tuples, may have received a " + "list of lists instead. In the future that will raise an error", + FutureWarning, stacklevel=2) + return _array + else: + pass - res = retval.view(recarray) + retval = sb.array(recList, dtype=descr) + if shape is not None and retval.shape != shape: + retval.shape = shape - return res + return retval.view(recarray) def fromstring(datastring, dtype=None, shape=None, offset=0, formats=None, diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 691e56d80..3c1744519 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -54,6 +54,14 @@ class TestFromrecords(object): assert_equal(r1, r2) + def test_fromrecords_list_of_lists(self): + # gh-10870 : For numpy 1.14 we keep the deprecated behavior + # that 1d list-of-lists input is accepted by fromrecords + expected = np.rec.array([(1, 1.5), (2, 2.5)], dtype='i8,f8') + with assert_warns(FutureWarning): + r = np.rec.fromrecords([[1, 1.5], [2, 2.5]], dtype='i8,f8') + assert_equal(r, expected) + def test_method_array(self): r = np.rec.array(b'abcdefg' * 100, formats='i2,a3,i4', shape=3, byteorder='big') assert_equal(r[1].item(), (25444, b'efg', 1633837924)) |