Skip to content

Commit

Permalink
BUG: Adding a columns to a Frame with RangeIndex columns using a non-…
Browse files Browse the repository at this point in the history
…scalar key (#52877)
  • Loading branch information
topper-123 committed Apr 23, 2023
1 parent 4dd071e commit 09b1966
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.1.rst
Expand Up @@ -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:
Expand Down Expand Up @@ -56,6 +57,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 :meth:`RangeIndex.get_loc`, like it does for :meth:`Index.get_loc`. Previously it raised an ``InvalidIndexError`` (:issue:`52652`).

.. ---------------------------------------------------------------------------
.. _whatsnew_201.contributors:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/range.py
Expand Up @@ -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)

Expand Down
33 changes: 30 additions & 3 deletions pandas/tests/indexes/test_indexing.py
Expand Up @@ -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,
Expand All @@ -29,7 +32,6 @@
MultiIndex,
NaT,
PeriodIndex,
RangeIndex,
TimedeltaIndex,
)
import pandas._testing as tm
Expand Down Expand Up @@ -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 = "<E.X1: 'x1'>"
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(
Expand All @@ -187,7 +215,6 @@ def test_get_loc_generator(self, index):
DatetimeIndex,
TimedeltaIndex,
PeriodIndex,
RangeIndex,
IntervalIndex,
MultiIndex,
),
Expand Down

0 comments on commit 09b1966

Please sign in to comment.