summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-07-01 08:38:25 -0600
committerGitHub <noreply@github.com>2017-07-01 08:38:25 -0600
commit0e4253526c727f50696f6233fee3d50a419ba9fe (patch)
tree7ce74ce6b6b13957d2964fdfc694812ced475230 /numpy
parent84f987c74209694b9cefe987154db404ad3f42e1 (diff)
parent1b3e6f1d8dad0b3b7d8a0891dcb2050cdc023075 (diff)
downloadnumpy-0e4253526c727f50696f6233fee3d50a419ba9fe.tar.gz
Merge pull request #9341 from eric-wieser/resize-empty
BUG: np.resize discards empty shapes
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/fromnumeric.py12
-rw-r--r--numpy/core/tests/test_numeric.py6
2 files changed, 11 insertions, 7 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index a8c2fd2fb..4922fc3e4 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -1120,18 +1120,16 @@ def resize(a, new_shape):
new_shape = (new_shape,)
a = ravel(a)
Na = len(a)
- if not Na:
- return mu.zeros(new_shape, a.dtype)
total_size = um.multiply.reduce(new_shape)
+ if Na == 0 or total_size == 0:
+ return mu.zeros(new_shape, a.dtype)
+
n_copies = int(total_size / Na)
extra = total_size % Na
- if total_size == 0:
- return a[:0]
-
if extra != 0:
- n_copies = n_copies+1
- extra = Na-extra
+ n_copies = n_copies + 1
+ extra = Na - extra
a = concatenate((a,)*n_copies)
if extra > 0:
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index a5149d4f7..1e51c4a61 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -34,6 +34,12 @@ class TestResize(TestCase):
assert_array_equal(Ar, np.array([]))
assert_equal(A.dtype, Ar.dtype)
+ Ar = np.resize(A, (0, 2))
+ assert_equal(Ar.shape, (0, 2))
+
+ Ar = np.resize(A, (2, 0))
+ assert_equal(Ar.shape, (2, 0))
+
def test_reshape_from_zero(self):
# See also gh-6740
A = np.zeros(0, dtype=[('a', np.float32, 1)])