summaryrefslogtreecommitdiff
path: root/numpy/oldnumeric/ma.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/oldnumeric/ma.py')
-rw-r--r--numpy/oldnumeric/ma.py68
1 files changed, 47 insertions, 21 deletions
diff --git a/numpy/oldnumeric/ma.py b/numpy/oldnumeric/ma.py
index 732e8447a..fbc0aca27 100644
--- a/numpy/oldnumeric/ma.py
+++ b/numpy/oldnumeric/ma.py
@@ -1,4 +1,5 @@
"""MA: a facility for dealing with missing observations
+
MA is generally used as a numpy.array look-alike.
by Paul F. Dubois.
@@ -8,18 +9,29 @@ Adapted for numpy_core 2005 by Travis Oliphant and
(mainly) Paul Dubois.
"""
-import types, sys
+from __future__ import division, absolute_import, print_function
+
+import sys
+import types
+import warnings
+from functools import reduce
import numpy.core.umath as umath
import numpy.core.fromnumeric as fromnumeric
+import numpy.core.numeric as numeric
from numpy.core.numeric import newaxis, ndarray, inf
from numpy.core.fromnumeric import amax, amin
from numpy.core.numerictypes import bool_, typecodes
import numpy.core.numeric as numeric
-import warnings
+from numpy.compat import bytes, long
if sys.version_info[0] >= 3:
- from functools import reduce
+ _MAXINT = sys.maxsize
+ _MININT = -sys.maxsize - 1
+else:
+ _MAXINT = sys.maxint
+ _MININT = -sys.maxint - 1
+
# Ufunc domain lookup for __array_wrap__
ufunc_domain = {}
@@ -83,13 +95,13 @@ default_object_fill_value = '?'
def default_fill_value (obj):
"Function to calculate default fill value for an object."
- if isinstance(obj, types.FloatType):
+ if isinstance(obj, float):
return default_real_fill_value
- elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType):
+ elif isinstance(obj, int) or isinstance(obj, long):
return default_integer_fill_value
- elif isinstance(obj, types.StringType):
+ elif isinstance(obj, bytes):
return default_character_fill_value
- elif isinstance(obj, types.ComplexType):
+ elif isinstance(obj, complex):
return default_complex_fill_value
elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray):
x = obj.dtype.char
@@ -109,33 +121,33 @@ def default_fill_value (obj):
def minimum_fill_value (obj):
"Function to calculate default fill value suitable for taking minima."
- if isinstance(obj, types.FloatType):
+ if isinstance(obj, float):
return numeric.inf
- elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType):
- return sys.maxint
+ elif isinstance(obj, int) or isinstance(obj, long):
+ return _MAXINT
elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray):
x = obj.dtype.char
if x in typecodes['Float']:
return numeric.inf
if x in typecodes['Integer']:
- return sys.maxint
+ return _MAXINT
if x in typecodes['UnsignedInteger']:
- return sys.maxint
+ return _MAXINT
else:
raise TypeError('Unsuitable type for calculating minimum.')
def maximum_fill_value (obj):
"Function to calculate default fill value suitable for taking maxima."
- if isinstance(obj, types.FloatType):
+ if isinstance(obj, float):
return -inf
- elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType):
- return -sys.maxint
+ elif isinstance(obj, int) or isinstance(obj, long):
+ return -_MAXINT
elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray):
x = obj.dtype.char
if x in typecodes['Float']:
return -inf
if x in typecodes['Integer']:
- return -sys.maxint
+ return -_MAXINT
if x in typecodes['UnsignedInteger']:
return 0
else:
@@ -229,7 +241,7 @@ def filled (a, value = None):
return a.filled(value)
elif isinstance(a, ndarray) and a.flags['CONTIGUOUS']:
return a
- elif isinstance(a, types.DictType):
+ elif isinstance(a, dict):
return numeric.array(a, 'O')
else:
return numeric.array(a)
@@ -863,6 +875,18 @@ array(data = %(data)s,
return bool(m is not nomask and m.any()
or d is not nomask and d.any())
+ def __bool__(self):
+ """returns true if any element is non-zero or masked
+
+ """
+ # XXX: This changes bool conversion logic from MA.
+ # XXX: In MA bool(a) == len(a) != 0, but in numpy
+ # XXX: scalars do not have len
+ m = self._mask
+ d = self._data
+ return bool(m is not nomask and m.any()
+ or d is not nomask and d.any())
+
def __len__ (self):
"""Return length of first dimension. This is weird but Python's
slicing behavior depends on it."""
@@ -1521,7 +1545,7 @@ def new_repeat(a, repeats, axis=None):
telling how many times to repeat each element.
"""
af = filled(a)
- if isinstance(repeats, types.IntType):
+ if isinstance(repeats, int):
if axis is None:
num = af.size
else:
@@ -2131,11 +2155,13 @@ def asarray(data, dtype=None):
# Add methods to support ndarray interface
# XXX: I is better to to change the masked_*_operation adaptors
# XXX: to wrap ndarray methods directly to create ma.array methods.
-from types import MethodType
+
def _m(f):
- return MethodType(f, None, array)
+ return types.MethodType(f, None, array)
+
def not_implemented(*args, **kwds):
raise NotImplementedError("not yet implemented for numpy.ma arrays")
+
array.all = _m(alltrue)
array.any = _m(sometrue)
array.argmax = _m(argmax)
@@ -2255,7 +2281,7 @@ array.std = _m(_std)
array.view = _m(not_implemented)
array.round = _m(around)
-del _m, MethodType, not_implemented
+del _m, not_implemented
masked = MaskedArray(0, int, mask=1)