Skip to content

Commit 793b635

Browse files
authored
CLN: Add comment and clarify if condition in indexing (#37960)
* Implement review comments * Change test names
1 parent 492c5e0 commit 793b635

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ def _setitem_single_block(self, indexer, value, name: str):
18211821
return
18221822

18231823
indexer = maybe_convert_ix(*indexer)
1824-
if isinstance(value, ABCSeries) and name != "iloc" or isinstance(value, dict):
1824+
if (isinstance(value, ABCSeries) and name != "iloc") or isinstance(value, dict):
18251825
# TODO(EA): ExtensionBlock.setitem this causes issues with
18261826
# setting for extensionarrays that store dicts. Need to decide
18271827
# if it's worth supporting that.
@@ -1859,6 +1859,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
18591859
if index.is_unique:
18601860
new_indexer = index.get_indexer([new_index[-1]])
18611861
if (new_indexer != -1).any():
1862+
# We get only here with loc, so can hard code
18621863
return self._setitem_with_indexer(new_indexer, value, "loc")
18631864

18641865
# this preserves dtype of the value

pandas/tests/indexing/test_iloc.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,15 @@ def test_iloc_setitem_bool_indexer(self, klass):
818818
tm.assert_frame_equal(df, expected)
819819

820820
@pytest.mark.parametrize("indexer", [[1], slice(1, 2)])
821-
def test_setitem_iloc_pure_position_based(self, indexer):
821+
def test_iloc_setitem_pure_position_based(self, indexer):
822822
# GH#22046
823823
df1 = DataFrame({"a2": [11, 12, 13], "b2": [14, 15, 16]})
824824
df2 = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
825825
df2.iloc[:, indexer] = df1.iloc[:, [0]]
826826
expected = DataFrame({"a": [1, 2, 3], "b": [11, 12, 13], "c": [7, 8, 9]})
827827
tm.assert_frame_equal(df2, expected)
828828

829-
def test_setitem_iloc_dictionary_value(self):
829+
def test_iloc_setitem_dictionary_value(self):
830830
# GH#37728
831831
df = DataFrame({"x": [1, 2], "y": [2, 2]})
832832
rhs = dict(x=9, y=99)
@@ -1000,7 +1000,7 @@ def test_iloc_getitem_nonunique(self):
10001000
ser = Series([0, 1, 2], index=[0, 1, 0])
10011001
assert ser.iloc[2] == 2
10021002

1003-
def test_setitem_iloc_pure_position_based(self):
1003+
def test_iloc_setitem_pure_position_based(self):
10041004
# GH#22046
10051005
ser1 = Series([1, 2, 3])
10061006
ser2 = Series([4, 5, 6], index=[1, 0, 2])

0 commit comments

Comments
 (0)