diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 74a4974cef1da..b44cd5f9a1369 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 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: @@ -57,6 +58,7 @@ Other - Bug in :class:`Index` where creating or converting to numpy string dtypes would raise ``NotImplementedError`` (:issue:`50127`) - 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 :meth:`RangeIndex.get_loc`, like it does for :meth:`Index.get_loc`. Previously it raised 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..3bc55786e1d2f 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): + # GH52877 + 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, ),