Skip to content

Commit 3ea4638

Browse files
authored
BUG: Raise TypeError for scalar indexer in take (#44763)
* BUG: Raise TypeError for scalar indexer in take * Make requested changes Use pandas is_scalar, remove deprecated constructor, add GH issue number to test * Add whatsnew entry
1 parent 6b9e93a commit 3ea4638

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Other enhancements
230230
- Implemented :meth:`IntervalArray.min`, :meth:`IntervalArray.max`, as a result of which ``min`` and ``max`` now work for :class:`IntervalIndex`, :class:`Series` and :class:`DataFrame` with ``IntervalDtype`` (:issue:`44746`)
231231
- :meth:`UInt64Index.map` now retains ``dtype`` where possible (:issue:`44609`)
232232
- :meth:`read_json` can now parse unsigned long long integers (:issue:`26068`)
233+
- :meth:`DataFrame.take` now raises a ``TypeError`` when passed a scalar for the indexer (:issue:`42875`)
233234
-
234235

235236

pandas/core/indexes/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,8 @@ def take(
10661066
):
10671067
if kwargs:
10681068
nv.validate_take((), kwargs)
1069+
if is_scalar(indices):
1070+
raise TypeError("Expected indices to be array-like")
10691071
indices = ensure_platform_int(indices)
10701072
allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices)
10711073

pandas/tests/indexes/test_indexing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def test_take(self, index):
7070
with pytest.raises(AttributeError, match=msg):
7171
index.freq
7272

73+
def test_take_indexer_type(self):
74+
# GH#42875
75+
integer_index = Index([0, 1, 2, 3])
76+
scalar_index = 1
77+
msg = "Expected indices to be array-like"
78+
with pytest.raises(TypeError, match=msg):
79+
integer_index.take(scalar_index)
80+
7381
def test_take_minus1_without_fill(self, index):
7482
# -1 does not get treated as NA unless allow_fill=True is passed
7583
if len(index) == 0:

0 commit comments

Comments
 (0)