Skip to content

Commit 38ab752

Browse files
jbrockmendeljreback
authored andcommitted
BUG: fix index validation in algos.take (#27079)
1 parent f8b0b9f commit 38ab752

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ def take(arr, indices, axis=0, allow_fill=False, fill_value=None):
15261526

15271527
if allow_fill:
15281528
# Pandas style, -1 means NA
1529-
validate_indices(indices, len(arr))
1529+
validate_indices(indices, arr.shape[axis])
15301530
result = take_1d(arr, indices, axis=axis, allow_fill=True,
15311531
fill_value=fill_value)
15321532
else:

pandas/core/arrays/sparse.py

-9
Original file line numberDiff line numberDiff line change
@@ -1868,15 +1868,6 @@ def _maybe_to_dense(obj):
18681868
return obj
18691869

18701870

1871-
def _maybe_to_sparse(array):
1872-
"""
1873-
array must be SparseSeries or SparseArray
1874-
"""
1875-
if isinstance(array, ABCSparseSeries):
1876-
array = array.array.copy()
1877-
return array
1878-
1879-
18801871
def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
18811872
"""
18821873
Convert ndarray to sparse format

pandas/tests/test_take.py

+5
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ def test_take_axis_1(self):
420420
expected = np.array([[0, 0], [3, 0], [6, 0], [9, 0]])
421421
tm.assert_numpy_array_equal(result, expected)
422422

423+
# GH#26976 make sure we validate along the correct axis
424+
with pytest.raises(IndexError, match="indices are out-of-bounds"):
425+
algos.take(arr, [0, 3], axis=1, allow_fill=True,
426+
fill_value=0)
427+
423428

424429
class TestExtensionTake:
425430
# The take method found in pd.api.extensions

0 commit comments

Comments
 (0)