Skip to content

Commit c7fa611

Browse files
authored
BUG: bug in Index._should_fallback_to_positional (#51241)
* BUG: big in Index._should_fallback_to_positional * add gh number * fix perf
1 parent aee3455 commit c7fa611

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,8 @@ Indexing
12531253
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
12541254
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
12551255
- Bug in :meth:`DataFrame.loc` raising ``IndexError`` when setting values for a pyarrow-backed column with a non-scalar indexer (:issue:`50085`)
1256+
- Bug in :meth:`DataFrame.__getitem__`, :meth:`Series.__getitem__`, :meth:`DataFrame.__setitem__` and :meth:`Series.__setitem__`
1257+
when indexing on indexes with extension float dtypes (:class:`Float64` & :class:`Float64`) or complex dtypes using integers (:issue:`51053`)
12561258
- Bug in :meth:`DataFrame.loc` modifying object when setting incompatible value with an empty indexer (:issue:`45981`)
12571259
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
12581260
- 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`)

pandas/core/indexes/base.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5701,9 +5701,12 @@ def _should_fallback_to_positional(self) -> bool:
57015701
"""
57025702
Should an integer key be treated as positional?
57035703
"""
5704-
if isinstance(self.dtype, np.dtype) and self.dtype.kind in ["i", "u", "f"]:
5705-
return False
5706-
return not self._holds_integer()
5704+
return self.inferred_type not in {
5705+
"integer",
5706+
"mixed-integer",
5707+
"floating",
5708+
"complex",
5709+
}
57075710

57085711
_index_shared_docs[
57095712
"get_indexer_non_unique"

pandas/tests/frame/indexing/test_indexing.py

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ def test_getitem(self, float_frame):
5757
with pytest.raises(KeyError, match="random"):
5858
float_frame["random"]
5959

60+
def test_getitem_numeric_should_not_fallback_to_positional(self, any_numeric_dtype):
61+
# GH51053
62+
dtype = any_numeric_dtype
63+
idx = Index([1, 0, 1], dtype=dtype)
64+
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=idx)
65+
result = df[1]
66+
expected = DataFrame([[1, 3], [4, 6]], columns=Index([1, 1], dtype=dtype))
67+
tm.assert_frame_equal(result, expected, check_exact=True)
68+
6069
def test_getitem2(self, float_frame):
6170

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

83+
def test_setitem_numeric_should_not_fallback_to_positional(self, any_numeric_dtype):
84+
# GH51053
85+
dtype = any_numeric_dtype
86+
idx = Index([1, 0, 1], dtype=dtype)
87+
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=idx)
88+
df[1] = 10
89+
expected = DataFrame([[10, 2, 10], [10, 5, 10]], columns=idx)
90+
tm.assert_frame_equal(df, expected, check_exact=True)
91+
7492
def test_setitem_list(self, float_frame):
7593

7694
float_frame["E"] = "foo"

pandas/tests/series/indexing/test_indexing.py

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ def test_basic_indexing():
4545
s[5] = 0
4646

4747

48+
def test_getitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
49+
# GH51053
50+
dtype = any_numeric_dtype
51+
idx = Index([1, 0, 1], dtype=dtype)
52+
ser = Series(range(3), index=idx)
53+
result = ser[1]
54+
expected = Series([0, 2], index=Index([1, 1], dtype=dtype))
55+
tm.assert_series_equal(result, expected, check_exact=True)
56+
57+
58+
def test_setitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
59+
# GH51053
60+
dtype = any_numeric_dtype
61+
idx = Index([1, 0, 1], dtype=dtype)
62+
ser = Series(range(3), index=idx)
63+
ser[1] = 10
64+
expected = Series([10, 1, 10], index=idx)
65+
tm.assert_series_equal(ser, expected, check_exact=True)
66+
67+
4868
def test_basic_getitem_with_labels(datetime_series):
4969
indices = datetime_series.index[[5, 10, 15]]
5070

0 commit comments

Comments
 (0)