summaryrefslogtreecommitdiff
path: root/numpy/lib/shape_base.py
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2013-12-20 16:35:00 +0100
committerSebastian Berg <sebastian@sipsolutions.net>2013-12-20 16:43:11 +0100
commitc91b4c3703eef84eca84063994d07332020c7707 (patch)
treede60bf6d9d7c77c243ac48ab934873f054a9e1ff /numpy/lib/shape_base.py
parent056ab73e567b8dae84055108dee6166d637baa57 (diff)
downloadnumpy-c91b4c3703eef84eca84063994d07332020c7707.tar.gz
BUG: Fix array_split empty array type and add FutureWarning
Fixes the result type of empty output arrays. The FutureWarning warns about changed behaviour. A "kludge" was introduced into array split converting converting the result of something like: >>> np.array_split(np.array([[1, 1]]), 2) [array([[1, 1]]), array([], dtype=int64)] instead of retaining the shape of the empty/second array to be (0, 2). A FutureWarning is now raised when such a replacement occurs. Closes gh-612
Diffstat (limited to 'numpy/lib/shape_base.py')
-rw-r--r--numpy/lib/shape_base.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py
index 1363a3213..4fdaba349 100644
--- a/numpy/lib/shape_base.py
+++ b/numpy/lib/shape_base.py
@@ -4,6 +4,8 @@ __all__ = ['column_stack', 'row_stack', 'dstack', 'array_split', 'split', 'hspli
'vsplit', 'dsplit', 'apply_over_axes', 'expand_dims',
'apply_along_axis', 'kron', 'tile', 'get_array_wrap']
+import warnings
+
import numpy.core.numeric as _nx
from numpy.core.numeric import asarray, zeros, newaxis, outer, \
concatenate, isscalar, array, asanyarray
@@ -347,9 +349,9 @@ def dstack(tup):
def _replace_zero_by_x_arrays(sub_arys):
for i in range(len(sub_arys)):
if len(_nx.shape(sub_arys[i])) == 0:
- sub_arys[i] = _nx.array([])
+ sub_arys[i] = _nx.empty(0, dtype=sub_arys[i].dtype)
elif _nx.sometrue(_nx.equal(_nx.shape(sub_arys[i]), 0)):
- sub_arys[i] = _nx.array([])
+ sub_arys[i] = _nx.empty(0, dtype=sub_arys[i].dtype)
return sub_arys
def array_split(ary,indices_or_sections,axis = 0):
@@ -395,11 +397,15 @@ def array_split(ary,indices_or_sections,axis = 0):
st = div_points[i]; end = div_points[i+1]
sub_arys.append(_nx.swapaxes(sary[st:end], axis, 0))
- # there is a weird issue with array slicing that allows
- # 0x10 arrays and other such things. The following kludge is needed
- # to get around this issue.
- sub_arys = _replace_zero_by_x_arrays(sub_arys)
- # end kludge.
+ # This "kludge" was introduced here to replace arrays shaped (0, 10)
+ # or similar with an array shaped (0,).
+ # There seems no need for this, so give a FutureWarning to remove later.
+ if sub_arys[-1].size == 0 and sub_arys[-1].ndim != 1:
+ warnings.warn("in the future np.array_split will retain the shape of "
+ "arrays with a zero size, instead of replacing them by "
+ "`array([])`, which always has a shape of (0,).",
+ FutureWarning)
+ sub_arys = _replace_zero_by_x_arrays(sub_arys)
return sub_arys