summaryrefslogtreecommitdiff
path: root/numpy/ma
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-10-04 23:35:42 -0700
committerGitHub <noreply@github.com>2017-10-04 23:35:42 -0700
commit98e7efc12ccbc53bef8b6c4e98354b29dcede3d0 (patch)
tree2514b5350585c9628929637380c078803895f322 /numpy/ma
parent729df78cfff15adb1a78377f18050e91f919a724 (diff)
parentaa7b339d6172775db71834677d68ec89da1aaf4e (diff)
downloadnumpy-98e7efc12ccbc53bef8b6c4e98354b29dcede3d0.tar.gz
Merge pull request #9817 from kenogo/master
BUG: Added exception for casting numpy.ma.masked to long
Diffstat (limited to 'numpy/ma')
-rw-r--r--numpy/ma/core.py12
-rw-r--r--numpy/ma/tests/test_core.py6
2 files changed, 17 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 130817e7a..5f4d14a73 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -4230,6 +4230,18 @@ class MaskedArray(ndarray):
elif self._mask:
raise MaskError('Cannot convert masked element to a Python int.')
return int(self.item())
+
+ def __long__(self):
+ """
+ Convert to long.
+ """
+ if self.size > 1:
+ raise TypeError("Only length-1 arrays can be conveted "
+ "to Python scalars")
+ elif self._mask:
+ raise MaskError('Cannot convert masked element to a Python long.')
+ return long(self.item())
+
def get_imag(self):
"""
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 31a92388d..fd8d629f9 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -4858,10 +4858,14 @@ class TestMaskedConstant(object):
assert_raises(ValueError, operator.setitem, view.data, (), 1)
assert_raises(ValueError, operator.setitem, view.mask, (), False)
- @dec.knownfailureif(sys.version_info.major == 2, "See gh-9751")
def test_coercion_int(self):
a_i = np.zeros((), int)
assert_raises(MaskError, operator.setitem, a_i, (), np.ma.masked)
+ assert_raises(MaskError, int, np.ma.masked)
+
+ @dec.skipif(sys.version_info.major == 3, "long doesn't exist in Python 3")
+ def test_coercion_long(self):
+ assert_raises(MaskError, long, np.ma.masked)
def test_coercion_float(self):
a_f = np.zeros((), float)