diff options
author | Shota Kawabuchi <shota.kawabuchi+Github@gmail.com> | 2016-09-19 03:10:22 +0900 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2017-03-04 11:40:55 -0700 |
commit | 6a1a029614bd69da24c2bc6fe50669f170dc77b8 (patch) | |
tree | b0e33496c8d56ef9d79b4cd1ed74758ea3b7155e | |
parent | c6a583e763bf0b613f7811046d92d6030c4b150c (diff) | |
download | numpy-6a1a029614bd69da24c2bc6fe50669f170dc77b8.tar.gz |
BUG: Look up methods on MaskedArray in _frommethod
Fixes #8019
-rw-r--r-- | numpy/ma/core.py | 23 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 10 |
2 files changed, 19 insertions, 14 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 73f39fce1..bb1a0222d 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -6384,21 +6384,16 @@ class _frommethod: def __call__(self, a, *args, **params): if self.reversed: args = list(args) - arr = args[0] - args[0] = a - a = arr - # Get the method from the array (if possible) + a, args[0] = args[0], a + + marr = asanyarray(a) method_name = self.__name__ - method = getattr(a, method_name, None) - if method is not None: - return method(*args, **params) - # Still here ? Then a is not a MaskedArray - method = getattr(MaskedArray, method_name, None) - if method is not None: - return method(MaskedArray(a), *args, **params) - # Still here ? OK, let's call the corresponding np function - method = getattr(np, method_name) - return method(a, *args, **params) + method = getattr(type(marr), method_name, None) + if method is None: + # use the corresponding np function + method = getattr(np, method_name) + + return method(marr, *args, **params) all = _frommethod('all') diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 5a1ed2be8..4df48a2b0 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -433,6 +433,11 @@ class TestMaskedArray(TestCase): assert_not_equal(y._data.ctypes.data, x._data.ctypes.data) assert_not_equal(y._mask.ctypes.data, x._mask.ctypes.data) + def test_copy_on_python_builtins(self): + # Tests copy works on python builtins (issue#8019) + self.assertTrue(isMaskedArray(np.ma.copy([1,2,3]))) + self.assertTrue(isMaskedArray(np.ma.copy((1,2,3)))) + def test_copy_immutable(self): # Tests that the copy method is immutable, GitHub issue #5247 a = np.ma.array([1, 2, 3]) @@ -1032,6 +1037,11 @@ class TestMaskedArrayArithmetic(TestCase): assert_(res.dtype.type is np.intp) assert_raises(ValueError, ott.count, axis=1) + def test_count_on_python_builtins(self): + # Tests count works on python builtins (issue#8019) + assert_equal(3, count([1,2,3])) + assert_equal(2, count((1,2))) + def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d |