Closed
Description
Previously, when writing Numpy array subclasses, it was possible to override the behavior of np.mean
and np.median
by defining a mean
method on the sub-class. For example,
import numpy as np
print np.__version__
class MySubClass(np.ndarray):
def __new__(cls, input_array, info=None):
obj = np.asarray(input_array).view(cls)
obj.info = info
return obj
def __array_prepare__(self, obj, context=None):
print "IN PREPARE"
return obj
def __array_finalize__(self, obj):
if obj is None: return
self.info = getattr(obj, 'info', None)
def __array_wrap__(self, out_arr, context=None):
print "IN WRAP", context
return np.ndarray.__array_wrap__(self, out_arr, context)
def mean(self, axis=None, dtype=None, out=None):
print "IN MEAN"
def median(self, axis=None, dtype=None, out=None):
print "IN MEDIAN"
a = MySubClass([1,2,3])
np.median(a)
produced the following output:
1.7.1
IN MEAN
However, in 1.8.0rc1, it looks like none of the methods shown above get called:
1.8.0rc1
This means that it is no longer possible to override the behavior of median
, which makes things like quantity handing with units break. For instance:
In [1]: import quantities as u
In [2]: import numpy as np
In [4]: np.median(np.array([1,2,3]) * u.m)
Out[4]: 2.0
In [5]: np.mean(np.array([1,2,3]) * u.m)
Out[5]: array(2.0) * m
or
In [3]: import numpy as np
In [4]: from astropy import units as u
In [5]: np.median(np.array([1,2,3]) * u.m)
Out[5]: 2.0
In [6]: np.mean(np.array([1,2,3]) * u.m)
Out[6]: <Quantity 2.0 m>
It would be great if this could be fixed by 1.8.0 final, as this will otherwise break a lot of unit-handing code.
Metadata
Metadata
Assignees
Labels
No labels