Skip to content

Commit

Permalink
BUG: bug in Index._should_fallback_to_positional (#51241)
Browse files Browse the repository at this point in the history
* BUG: big in Index._should_fallback_to_positional

* add gh number

* fix perf
  • Loading branch information
topper-123 authored Feb 13, 2023
1 parent aee3455 commit c7fa611
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@ Indexing
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
- Bug in :meth:`DataFrame.loc` raising ``IndexError`` when setting values for a pyarrow-backed column with a non-scalar indexer (:issue:`50085`)
- Bug in :meth:`DataFrame.__getitem__`, :meth:`Series.__getitem__`, :meth:`DataFrame.__setitem__` and :meth:`Series.__setitem__`
when indexing on indexes with extension float dtypes (:class:`Float64` & :class:`Float64`) or complex dtypes using integers (:issue:`51053`)
- Bug in :meth:`DataFrame.loc` modifying object when setting incompatible value with an empty indexer (:issue:`45981`)
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5701,9 +5701,12 @@ def _should_fallback_to_positional(self) -> bool:
"""
Should an integer key be treated as positional?
"""
if isinstance(self.dtype, np.dtype) and self.dtype.kind in ["i", "u", "f"]:
return False
return not self._holds_integer()
return self.inferred_type not in {
"integer",
"mixed-integer",
"floating",
"complex",
}

_index_shared_docs[
"get_indexer_non_unique"
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def test_getitem(self, float_frame):
with pytest.raises(KeyError, match="random"):
float_frame["random"]

def test_getitem_numeric_should_not_fallback_to_positional(self, any_numeric_dtype):
# GH51053
dtype = any_numeric_dtype
idx = Index([1, 0, 1], dtype=dtype)
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=idx)
result = df[1]
expected = DataFrame([[1, 3], [4, 6]], columns=Index([1, 1], dtype=dtype))
tm.assert_frame_equal(result, expected, check_exact=True)

def test_getitem2(self, float_frame):

df = float_frame.copy()
Expand All @@ -71,6 +80,15 @@ def test_getitem2(self, float_frame):
res = df["@awesome_domain"]
tm.assert_numpy_array_equal(ad, res.values)

def test_setitem_numeric_should_not_fallback_to_positional(self, any_numeric_dtype):
# GH51053
dtype = any_numeric_dtype
idx = Index([1, 0, 1], dtype=dtype)
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=idx)
df[1] = 10
expected = DataFrame([[10, 2, 10], [10, 5, 10]], columns=idx)
tm.assert_frame_equal(df, expected, check_exact=True)

def test_setitem_list(self, float_frame):

float_frame["E"] = "foo"
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def test_basic_indexing():
s[5] = 0


def test_getitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
# GH51053
dtype = any_numeric_dtype
idx = Index([1, 0, 1], dtype=dtype)
ser = Series(range(3), index=idx)
result = ser[1]
expected = Series([0, 2], index=Index([1, 1], dtype=dtype))
tm.assert_series_equal(result, expected, check_exact=True)


def test_setitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
# GH51053
dtype = any_numeric_dtype
idx = Index([1, 0, 1], dtype=dtype)
ser = Series(range(3), index=idx)
ser[1] = 10
expected = Series([10, 1, 10], index=idx)
tm.assert_series_equal(ser, expected, check_exact=True)


def test_basic_getitem_with_labels(datetime_series):
indices = datetime_series.index[[5, 10, 15]]

Expand Down

0 comments on commit c7fa611

Please sign in to comment.