From 1e45ea470eaae8d23c161f78931e946847e2b7e4 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 24 Feb 2018 23:32:29 -0800 Subject: BUG: Make np.partition and np.sort work on np.matrix when axis=None Both were making the normally valid assumption that flatten actually flattens, which turns out to be false for matrices. Old behavior: >>> a = np.matrix([[1, 2, 0]]) >>> np.partition(a, 1, axis=None) ValueError: kth(=1) out of bounds (1) >>> np.sort(a, axis=None) matrix([[1, 2, 0]]) --- numpy/core/fromnumeric.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy/core/fromnumeric.py') diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 43584349f..4dfeb35ca 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -657,8 +657,9 @@ def partition(a, kth, axis=-1, kind='introselect', order=None): """ if axis is None: + # flatten returns (1, N) for np.matrix, so always use the last axis a = asanyarray(a).flatten() - axis = 0 + axis = -1 else: a = asanyarray(a).copy(order="K") a.partition(kth, axis=axis, kind=kind, order=order) @@ -840,8 +841,9 @@ def sort(a, axis=-1, kind='quicksort', order=None): """ if axis is None: + # flatten returns (1, N) for np.matrix, so always use the last axis a = asanyarray(a).flatten() - axis = 0 + axis = -1 else: a = asanyarray(a).copy(order="K") a.sort(axis=axis, kind=kind, order=order) -- cgit v1.2.1