From 29bc3c3c0385782d719fdd1d1feeb787a373fb19 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 27 Jun 2019 11:50:26 -0500 Subject: [PATCH 1/2] BUG: fix algos.take index validation, closes #26976 --- pandas/core/algorithms.py | 2 +- pandas/tests/test_take.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index ff1313c21d96f..77664b3fa73d0 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1526,7 +1526,7 @@ def take(arr, indices, axis=0, allow_fill=False, fill_value=None): if allow_fill: # Pandas style, -1 means NA - validate_indices(indices, len(arr)) + validate_indices(indices, arr.shape[axis]) result = take_1d(arr, indices, axis=axis, allow_fill=True, fill_value=fill_value) else: diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index a60fce99eb9a7..afcc90a1c8e74 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -420,6 +420,11 @@ def test_take_axis_1(self): expected = np.array([[0, 0], [3, 0], [6, 0], [9, 0]]) tm.assert_numpy_array_equal(result, expected) + # GH#26976 make sure we validate along the correct axis + with pytest.raises(IndexError, match="indices are out-of-bounds"): + algos.take(arr, [0, 3], axis=1, allow_fill=True, + fill_value=0) + class TestExtensionTake: # The take method found in pd.api.extensions From b6c03e7d1fa9d6b22114984da35b52e51161a61c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 27 Jun 2019 11:51:27 -0500 Subject: [PATCH 2/2] CLN: remove unused maybe_to_sparse --- pandas/core/arrays/sparse.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index d692fe6d7cabe..1c4ab70fa9332 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1868,15 +1868,6 @@ def _maybe_to_dense(obj): return obj -def _maybe_to_sparse(array): - """ - array must be SparseSeries or SparseArray - """ - if isinstance(array, ABCSparseSeries): - array = array.array.copy() - return array - - def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False): """ Convert ndarray to sparse format