summaryrefslogtreecommitdiff
path: root/numpy/lib/tests
diff options
context:
space:
mode:
authorHuon Wilson <Huon.Wilson@data61.csiro.au>2020-03-12 20:07:33 -0400
committerWarren Weckesser <warren.weckesser@gmail.com>2020-03-15 11:35:39 -0400
commit353bf737fb84fe285b430e6496aa7537f56a674f (patch)
tree4bbe653d4b7695cd6c1255a43b85c175a5eb2788 /numpy/lib/tests
parent26527695f9ee7b7bc3fdd3af67d7e8b068cb1e0b (diff)
downloadnumpy-353bf737fb84fe285b430e6496aa7537f56a674f.tar.gz
TST: lib: Add a unit test for np.unique applied to arrays with a length 0 axis.
This code is from github user huonw, from this PR: https://github.com/numpy/numpy/pull/15565
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r--numpy/lib/tests/test_arraysetops.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py
index 851fd31ea..f7d91a26a 100644
--- a/numpy/lib/tests/test_arraysetops.py
+++ b/numpy/lib/tests/test_arraysetops.py
@@ -563,6 +563,41 @@ class TestUnique:
result = np.array([[-0.0, 0.0]])
assert_array_equal(unique(data, axis=0), result, msg)
+ def test_unique_axis_zeros(self):
+ # issue 15559
+ single_zero = np.empty(shape=(2, 0), dtype=np.int8)
+ uniq, idx, inv, cnt = unique(single_zero, axis=0, return_index=True,
+ return_inverse=True, return_counts=True)
+
+ # there's 1 element of shape (0,) along axis 0
+ assert_equal(uniq.dtype, single_zero.dtype)
+ assert_array_equal(uniq, np.empty(shape=(1, 0)))
+ assert_array_equal(idx, np.array([0]))
+ assert_array_equal(inv, np.array([0, 0]))
+ assert_array_equal(cnt, np.array([2]))
+
+ # there's 0 elements of shape (2,) along axis 1
+ uniq, idx, inv, cnt = unique(single_zero, axis=1, return_index=True,
+ return_inverse=True, return_counts=True)
+
+ assert_equal(uniq.dtype, single_zero.dtype)
+ assert_array_equal(uniq, np.empty(shape=(2, 0)))
+ assert_array_equal(idx, np.array([]))
+ assert_array_equal(inv, np.array([]))
+ assert_array_equal(cnt, np.array([]))
+
+ # test a "complicated" shape
+ shape = (0, 2, 0, 3, 0, 4, 0)
+ multiple_zeros = np.empty(shape=shape)
+ for axis in range(len(shape)):
+ expected_shape = list(shape)
+ if shape[axis] == 0:
+ expected_shape[axis] = 0
+ else:
+ expected_shape[axis] = 1
+
+ assert_array_equal(unique(multiple_zeros, axis=axis), np.empty(shape=expected_shape))
+
def test_unique_masked(self):
# issue 8664
x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0], dtype='uint8')