summaryrefslogtreecommitdiff
path: root/numpy/core/fromnumeric.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-02-24 23:32:29 -0800
committerEric Wieser <wieser.eric@gmail.com>2018-02-25 13:31:01 -0800
commit1e45ea470eaae8d23c161f78931e946847e2b7e4 (patch)
tree3d43c1484ee6c1ebaf6d66d2f9203455926d8d78 /numpy/core/fromnumeric.py
parentd71b17d5aac95ad818dc78680e23f16bdbbf45dd (diff)
downloadnumpy-1e45ea470eaae8d23c161f78931e946847e2b7e4.tar.gz
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]])
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r--numpy/core/fromnumeric.py6
1 files changed, 4 insertions, 2 deletions
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)