8000 Merge pull request #3851 from juliantaylor/median-subclass · numpy/numpy@f665c61 · GitHub
[go: up one dir, main page]

Skip to content

Commit f665c61

Browse files
committed
Merge pull request #3851 from juliantaylor/median-subclass
BUG: preserve ndarray subclasses in median
2 parents d231626 + e6f0c90 commit f665c61

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

numpy/lib/function_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ def median(a, axis=None, out=None, overwrite_input=False):
27642764
>>> assert not np.all(a==b)
27652765
27662766
"""
2767-
a = np.asarray(a)
2767+
a = np.asanyarray(a)
27682768
if axis is not None and axis >= a.ndim:
27692769
raise IndexError(
27702770
"axis %d out of bounds (%d)" % (axis, a.ndim))

numpy/lib/tests/test_function_base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,19 @@ def test_array_like(self):
16211621
assert_almost_equal(np.median(x2), 2)
16221622
assert_allclose(np.median(x2, axis=0), x)
16231623

1624+
def test_subclass(self):
1625+
# gh-3846
1626+
class MySubClass(np.ndarray):
1627+
def __new__(cls, input_array, info=None):
1628+
obj = np.asarray(input_array).view(cls)
1629+
obj.info = info
1630+
return obj
1631+
1632+
def mean(self, axis=None, dtype=None, out=None):
1633+
return -7
1634+
1635+
a = MySubClass([1,2,3])
1636+
assert_equal(np.median(a), -7)
16241637

16251638
class TestAdd_newdoc_ufunc(TestCase):
16261639

0 commit comments

Comments
 (0)
0