diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-01-16 18:04:48 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-16 18:04:48 -0600 |
commit | 7838c8f24fe66375ce8efd2e47a8c78c839fb9d3 (patch) | |
tree | 67222d8a7fc0985b76f79a69f32f142000bfe19e | |
parent | e4c91ccbae79d693970fe3d54e8f771804f90c32 (diff) | |
parent | bc6d573a58bf8a8f4448f26ed220afff93df7c97 (diff) | |
download | numpy-7838c8f24fe66375ce8efd2e47a8c78c839fb9d3.tar.gz |
Merge pull request #15217 from eric-wieser/deprecate-shape-0
DEP: records: Deprecate treating shape=0 as shape=None
-rw-r--r-- | doc/release/upcoming_changes/15217.deprecation.rst | 11 | ||||
-rw-r--r-- | numpy/core/records.py | 35 |
2 files changed, 40 insertions, 6 deletions
diff --git a/doc/release/upcoming_changes/15217.deprecation.rst b/doc/release/upcoming_changes/15217.deprecation.rst new file mode 100644 index 000000000..e9dd0995d --- /dev/null +++ b/doc/release/upcoming_changes/15217.deprecation.rst @@ -0,0 +1,11 @@ +Passing ``shape=0`` to factory functions in ``numpy.rec`` is deprecated +----------------------------------------------------------------------- + +``0`` is treated as a special case by these functions, which aliases to +``None``. In future, ``0`` will not be a special case, and will be treated +as an array length like any other integer is. The affected functions are: + +* `numpy.core.records.fromarrays` +* `numpy.core.records.fromrecords` +* `numpy.core.records.fromstring` +* `numpy.core.records.fromfile` diff --git a/numpy/core/records.py b/numpy/core/records.py index 6717dc69b..d4aa2feb9 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -581,6 +581,18 @@ class recarray(ndarray): return self.setfield(val, *res) +def _deprecate_shape_0_as_None(shape): + if shape == 0: + warnings.warn( + "Passing `shape=0` to have the shape be inferred is deprecated, " + "and in future will be equivalent to `shape=(0,)`. To infer " + "the shape and suppress this warning, pass `shape=None` instead.", + FutureWarning, stacklevel=3) + return None + else: + return shape + + def fromarrays(arrayList, dtype=None, shape=None, formats=None, names=None, titles=None, aligned=False, byteorder=None): """ create a record array from a (flat) list of arrays @@ -598,10 +610,12 @@ def fromarrays(arrayList, dtype=None, shape=None, formats=None, arrayList = [sb.asarray(x) for x in arrayList] - if shape is None or shape == 0: - shape = arrayList[0].shape + # NumPy 1.19.0, 2020-01-01 + shape = _deprecate_shape_0_as_None(shape) - if isinstance(shape, int): + if shape is None: + shape = arrayList[0].shape + elif isinstance(shape, int): shape = (shape,) if formats is None and dtype is None: @@ -687,7 +701,9 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None, try: retval = sb.array(recList, dtype=descr) except (TypeError, ValueError): - if (shape is None or shape == 0): + # NumPy 1.19.0, 2020-01-01 + shape = _deprecate_shape_0_as_None(shape) + if shape is None: shape = len(recList) if isinstance(shape, (int, long)): shape = (shape,) @@ -726,7 +742,11 @@ def fromstring(datastring, dtype=None, shape=None, offset=0, formats=None, descr = format_parser(formats, names, titles, aligned, byteorder)._descr itemsize = descr.itemsize - if (shape is None or shape == 0 or shape == -1): + + # NumPy 1.19.0, 2020-01-01 + shape = _deprecate_shape_0_as_None(shape) + + if shape is None or shape == -1: shape = (len(datastring) - offset) // itemsize _array = recarray(shape, descr, buf=datastring, offset=offset) @@ -769,7 +789,10 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None, if dtype is None and formats is None: raise TypeError("fromfile() needs a 'dtype' or 'formats' argument") - if (shape is None or shape == 0): + # NumPy 1.19.0, 2020-01-01 + shape = _deprecate_shape_0_as_None(shape) + + if shape is None: shape = (-1,) elif isinstance(shape, (int, long)): shape = (shape,) |