Reputation: 18264
I have a masked array containing some real values and some duds. Via ipython, this is what I have set up:
In [31]: x
Out[31]:
masked_array(data=[--, --, --, 2.718281828459045, 3.141592653589793],
mask=[ True, True, True, False, False],
fill_value='?',
dtype=object)
Note: the reason dtype=object
is because this is an actual ring_buffer containing datetimes and float values. However, for this it should not matter.
I want to find the maximum and minimum of that array. The documentation points to the .min(…)
and .max(…)
methods to do just that. However, when I call them, I get this exception:
In [32]: x.max()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-32-031604a175b1> in <module>()
----> 1 x.max()
/home/usr/repos/proj/.tox/develop/lib/python2.7/site-packages/numpy/ma/core.pyc in max(self, axis, out, fill_value, keepdims)
5707 if out is None:
5708 result = self.filled(fill_value).max(
-> 5709 axis=axis, out=out, **kwargs).view(type(self))
5710 if result.ndim:
5711 # Set the mask
AttributeError: 'str' object has no attribute 'view'
What am I doing wrong?
Upvotes: 2
Views: 982
Reputation: 12156
So the fact you're using dtype=object
does matter. The masked array doesn't know how to properly handle these so just uses the standard numpy.array
methods. The default fill value for dtype=object
is '?'
, hence the error you get.
You should not be mixing datatypes in a numpy array. It defeats every advantage you get from them. Use lists (or pandas if dealing with different columns of different types) instead. max
will fail on a numpy array of floats and datetimes anyway.
x = array([datetime.datetime(2018, 8, 28, 8, 52, 2, 107691), 3.14, 2.78],
dtype=object)
x.max()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-152-031604a175b1> in <module>()
----> 1 x.max()
C:\Programs\Anaconda3\lib\site-packages\numpy\core\_methods.py in _amax(a, axis, out, keepdims)
24 # small reductions
25 def _amax(a, axis=None, out=None, keepdims=False):
---> 26 return umr_maximum(a, axis, None, out, keepdims)
27
28 def _amin(a, axis=None, out=None, keepdims=False):
TypeError: '>=' not supported between instances of 'datetime.datetime' and 'float'
Upvotes: 2