Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Adding a columns to a Frame with RangeIndex columns using a non-scalar key #52877

Merged
merged 3 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
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 @@ -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 :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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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