diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-01-18 18:09:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-18 18:09:25 -0700 |
commit | 7d2c9d1b32a2d76d28453705e6f8b589d7e273d7 (patch) | |
tree | 55f0d8875ba0695860069705d49d03cd810082d7 | |
parent | 8a772dd80929aa556c6c01b9025f3c1da0666938 (diff) | |
parent | 70e34252dc224ace1192cb8534fd55442afe3dfe (diff) | |
download | numpy-7d2c9d1b32a2d76d28453705e6f8b589d7e273d7.tar.gz |
Merge pull request #10342 from anaskhan96/union1d-fix
BUG: arrays not being flattened in `union1d`
-rw-r--r-- | numpy/lib/arraysetops.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_arraysetops.py | 8 | ||||
-rw-r--r-- | numpy/ma/extras.py | 2 | ||||
-rw-r--r-- | numpy/ma/tests/test_extras.py | 8 |
4 files changed, 18 insertions, 2 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 7e8bf2133..b1e74dc74 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -618,7 +618,7 @@ def union1d(ar1, ar2): >>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2])) array([1, 2, 3, 4, 6]) """ - return unique(np.concatenate((ar1, ar2))) + return unique(np.concatenate((ar1, ar2), axis=None)) def setdiff1d(ar1, ar2, assume_unique=False): """ diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index b4787838d..c2ba7ac86 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -247,6 +247,14 @@ class TestSetOps(object): c = union1d(a, b) assert_array_equal(c, ec) + # Tests gh-10340, arguments to union1d should be + # flattened if they are not already 1D + x = np.array([[0, 1, 2], [3, 4, 5]]) + y = np.array([0, 1, 2, 3, 4]) + ez = np.array([0, 1, 2, 3, 4, 5]) + z = union1d(x, y) + assert_array_equal(z, ez) + assert_array_equal([], union1d([], [])) def test_setdiff1d(self): diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 360d50d8a..99f5234d1 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -1209,7 +1209,7 @@ def union1d(ar1, ar2): numpy.union1d : Equivalent function for ndarrays. """ - return unique(ma.concatenate((ar1, ar2))) + return unique(ma.concatenate((ar1, ar2), axis=None)) def setdiff1d(ar1, ar2, assume_unique=False): diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 7687514fa..d1c1aa63e 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -1510,6 +1510,14 @@ class TestArraySetOps(object): test = union1d(a, b) control = array([1, 2, 3, 4, 5, 7, -1], mask=[0, 0, 0, 0, 0, 0, 1]) assert_equal(test, control) + + # Tests gh-10340, arguments to union1d should be + # flattened if they are not already 1D + x = array([[0, 1, 2], [3, 4, 5]], mask=[[0, 0, 0], [0, 0, 1]]) + y = array([0, 1, 2, 3, 4], mask=[0, 0, 0, 0, 1]) + ez = array([0, 1, 2, 3, 4, 5], mask=[0, 0, 0, 0, 0, 1]) + z = union1d(x, y) + assert_equal(z, ez) # assert_array_equal([], union1d([], [])) |