From 57d05577a3cd7450f5ffba31e4a411428dd2aacb Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sun, 23 Apr 2023 11:45:19 +0100 Subject: [PATCH 1/3] BUG: Adding a columns to a Frame using a non-scalar key --- doc/source/whatsnew/v2.0.1.rst | 2 ++ pandas/core/indexes/range.py | 2 ++ pandas/tests/indexes/test_indexing.py | 33 ++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 7bd1b8f963726..553e7e2e2a161 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -20,6 +20,7 @@ Fixed regressions - Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression in :meth:`SeriesGroupBy.agg` failing when grouping with categorical data, multiple groupings, ``as_index=False``, and a list of aggregations (:issue:`52760`) +- Fixed regression when adding a new column in a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: @@ -55,6 +56,7 @@ Other - :class:`Series` created from empty dicts had :attr:`~Series.index` of dtype ``object``. It is now a :class:`RangeIndex` (:issue:`52404`) - Implemented :meth:`Series.str.split` and :meth:`Series.str.rsplit` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`52401`) - Implemented most ``str`` accessor methods for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`52401`) +- Supplying a non-integer hashable key that tests ``False`` in :func:`api.types.is_scalar` now raises a ``KeyError`` for :class:`RangeIndex`, like :class:`Index` does. Previously it raises an ``InvalidIndexError`` (:issue:`52652`). .. --------------------------------------------------------------------------- .. _whatsnew_201.contributors: diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 10b3fa34da127..dd72ce30d290b 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -347,6 +347,8 @@ def get_loc(self, key): return self._range.index(new_key) except ValueError as err: raise KeyError(key) from err + if isinstance(key, Hashable): + raise KeyError(key) self._check_indexing_error(key) raise KeyError(key) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 52f09ac25873e..6f11bdc96c183 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -19,7 +19,10 @@ from pandas.errors import InvalidIndexError -from pandas.core.dtypes.common import is_float_dtype +from pandas.core.dtypes.common import ( + is_float_dtype, + is_scalar, +) from pandas import ( NA, @@ -29,7 +32,6 @@ MultiIndex, NaT, PeriodIndex, - RangeIndex, TimedeltaIndex, ) import pandas._testing as tm @@ -179,6 +181,32 @@ def test_get_loc_non_hashable(self, index): with pytest.raises((TypeError, InvalidIndexError), match="slice"): index.get_loc(slice(0, 1)) + def test_get_loc_non_scalar_hashable(self, index): + # GHXXXXX + from enum import Enum + + class E(Enum): + X1 = "x1" + + assert not is_scalar(E.X1) + + exc = KeyError + msg = "" + if isinstance( + index, + ( + DatetimeIndex, + TimedeltaIndex, + PeriodIndex, + IntervalIndex, + ), + ): + # TODO: make these more consistent? + exc = InvalidIndexError + msg = "E.X1" + with pytest.raises(exc, match=msg): + index.get_loc(E.X1) + def test_get_loc_generator(self, index): exc = KeyError if isinstance( @@ -187,7 +215,6 @@ def test_get_loc_generator(self, index): DatetimeIndex, TimedeltaIndex, PeriodIndex, - RangeIndex, IntervalIndex, MultiIndex, ), From bbdcf7186ae48f4389bf36f228962bbba3a9feb8 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sun, 23 Apr 2023 11:48:52 +0100 Subject: [PATCH 2/3] add GH number --- pandas/tests/indexes/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 6f11bdc96c183..3bc55786e1d2f 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -182,7 +182,7 @@ def test_get_loc_non_hashable(self, index): index.get_loc(slice(0, 1)) def test_get_loc_non_scalar_hashable(self, index): - # GHXXXXX + # GH52877 from enum import Enum class E(Enum): From 9214294280f470afb494f929038e2164899d26ef Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sun, 23 Apr 2023 12:17:26 +0100 Subject: [PATCH 3/3] fix text --- doc/source/whatsnew/v2.0.1.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 553e7e2e2a161..b0aa6b20c9281 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -20,7 +20,7 @@ Fixed regressions - Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression in :meth:`SeriesGroupBy.agg` failing when grouping with categorical data, multiple groupings, ``as_index=False``, and a list of aggregations (:issue:`52760`) -- Fixed regression when adding a new column in a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) +- Fixed regression when adding a new column to a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: @@ -56,7 +56,7 @@ Other - :class:`Series` created from empty dicts had :attr:`~Series.index` of dtype ``object``. It is now a :class:`RangeIndex` (:issue:`52404`) - Implemented :meth:`Series.str.split` and :meth:`Series.str.rsplit` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`52401`) - Implemented most ``str`` accessor methods for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`52401`) -- Supplying a non-integer hashable key that tests ``False`` in :func:`api.types.is_scalar` now raises a ``KeyError`` for :class:`RangeIndex`, like :class:`Index` does. Previously it raises an ``InvalidIndexError`` (:issue:`52652`). +- Supplying a non-integer hashable key that tests ``False`` in :func:`api.types.is_scalar` now raises a ``KeyError`` for :meth:`RangeIndex.get_loc`, like it does for :meth:`Index.get_loc`. Previously it raised an ``InvalidIndexError`` (:issue:`52652`). .. --------------------------------------------------------------------------- .. _whatsnew_201.contributors: