From 4aa4c020539165e5c618fc8f104b8cf821c43484 Mon Sep 17 00:00:00 2001 From: gfkang Date: Sat, 4 Dec 2021 18:37:19 -0500 Subject: [PATCH 1/3] BUG: Raise TypeError for scalar indexer in take --- pandas/core/indexes/base.py | 2 ++ pandas/tests/indexes/test_indexing.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9ce7c943214b3..020580e9974a2 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1066,6 +1066,8 @@ def take( ): if kwargs: nv.validate_take((), kwargs) + if np.isscalar(indices): + raise TypeError("Expected indices to be array-like") indices = ensure_platform_int(indices) allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 9acdd52178e0e..e79e9659f7927 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -70,6 +70,13 @@ def test_take(self, index): with pytest.raises(AttributeError, match=msg): index.freq + def test_take_indexer_type(self): + integer_index = Int64Index([0, 1, 2, 3]) + scalar_index = 1 + msg = "Expected indices to be array-like" + with pytest.raises(TypeError, match=msg): + integer_index.take(scalar_index) + def test_take_minus1_without_fill(self, index): # -1 does not get treated as NA unless allow_fill=True is passed if len(index) == 0: From fe719ae817ed892ba9827d74e1dedf80c9d42308 Mon Sep 17 00:00:00 2001 From: gfkang Date: Mon, 6 Dec 2021 13:54:30 -0500 Subject: [PATCH 2/3] Make requested changes Use pandas is_scalar, remove deprecated constructor, add GH issue number to test --- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/test_indexing.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 020580e9974a2..54a13c124c13a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1066,7 +1066,7 @@ def take( ): if kwargs: nv.validate_take((), kwargs) - if np.isscalar(indices): + if is_scalar(indices): raise TypeError("Expected indices to be array-like") indices = ensure_platform_int(indices) allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index e79e9659f7927..94b9ca62e08ed 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -71,7 +71,8 @@ def test_take(self, index): index.freq def test_take_indexer_type(self): - integer_index = Int64Index([0, 1, 2, 3]) + # GH#42875 + integer_index = Index([0, 1, 2, 3]) scalar_index = 1 msg = "Expected indices to be array-like" with pytest.raises(TypeError, match=msg): From a3deae2f858fcc8af4c3881dca42d5e2014251fb Mon Sep 17 00:00:00 2001 From: gfkang Date: Mon, 6 Dec 2021 14:35:05 -0500 Subject: [PATCH 3/3] Add whatsnew entry --- doc/source/whatsnew/v1.4.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 55f3bf334fa2e..78031fd0456f7 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -230,6 +230,7 @@ Other enhancements - 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`) - :meth:`UInt64Index.map` now retains ``dtype`` where possible (:issue:`44609`) - :meth:`read_json` can now parse unsigned long long integers (:issue:`26068`) +- :meth:`DataFrame.take` now raises a ``TypeError`` when passed a scalar for the indexer (:issue:`42875`) -