Skip to content

Commit abc4591

Browse files
committed
Fixed requested issues
1 parent cf27966 commit abc4591

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Indexing
329329
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
330330
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
331331
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
332-
- Bug in :func:`Series.iloc` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` ``Series`` causing the wrong indexes to be read from and written to (:issue:`17148`)
332+
- Bug in ``.iloc`` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` causing the wrong indexes to be read from and written to (:issue:`17148`)
333333

334334
I/O
335335
^^^

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _get_setitem_indexer(self, key):
147147

148148
axis = self.obj._get_axis(0)
149149

150-
if isinstance(axis, MultiIndex) and not is_integer(key):
150+
if isinstance(axis, MultiIndex) and self.name != 'iloc':
151151
try:
152152
return axis.get_loc(key)
153153
except Exception:

pandas/tests/indexing/test_iloc.py

+22-32
Original file line numberDiff line numberDiff line change
@@ -269,39 +269,29 @@ def test_iloc_setitem(self):
269269
expected = Series([0, 1, 0], index=[4, 5, 6])
270270
tm.assert_series_equal(s, expected)
271271

272-
def test_iloc_setitem_int_multiindex_series(self):
272+
@pytest.mark.parametrize(
273+
"data, indexes, values, expected_k", [
274+
([[1, 22, 5], [1, 33, 6]], [0, -1, 1], [2, 3, 1], [7, 10]),
275+
([[2, 22, 5], [2, 33, 6]], [0, -1, 1], [2, 3, 1], [7, 10]),
276+
([[1, 3, 7], [2, 4, 8]], [0, -1, 1], [1, 1, 10], [8, 19]),
277+
([[1, 11, 4], [2, 22, 5], [3, 33, 6]], [0, -1, 1], [4, 7, 10],
278+
[8, 15, 13])
279+
])
280+
def test_iloc_setitem_int_multiindex_series(
281+
self, data, indexes, values, expected_k):
273282
# GH17148
274-
def check_scenario(data, indexes, values, expected_k):
275-
df = pd.DataFrame(
276-
data=data,
277-
columns=['i', 'j', 'k'])
278-
df.set_index(['i', 'j'], inplace=True)
279-
280-
series = df.k.copy()
281-
for i, v in zip(indexes, values):
282-
series.iloc[i] += v
283-
284-
df.k = expected_k
285-
expected = df.k.copy()
286-
tm.assert_series_equal(series, expected)
287-
288-
check_scenario(
289-
data=[[1, 22, 5], [1, 33, 6]],
290-
indexes=[0, -1, 1],
291-
values=[2, 3, 1],
292-
expected_k=[7, 10])
293-
294-
check_scenario(
295-
data=[[1, 3, 7], [2, 4, 8]],
296-
indexes=[0, -1, 1],
297-
values=[1, 1, 10],
298-
expected_k=[8, 19])
299-
300-
check_scenario(
301-
data=[[1, 11, 4], [2, 22, 5], [3, 33, 6]],
302-
indexes=[0, -1, 1],
303-
values=[4, 7, 10],
304-
expected_k=[8, 15, 13])
283+
df = pd.DataFrame(
284+
data=data,
285+
columns=['i', 'j', 'k'])
286+
df.set_index(['i', 'j'], inplace=True)
287+
288+
series = df.k.copy()
289+
for i, v in zip(indexes, values):
290+
series.iloc[i] += v
291+
292+
df.k = expected_k
293+
expected = df.k.copy()
294+
tm.assert_series_equal(series, expected)
305295

306296
def test_iloc_setitem_list(self):
307297

0 commit comments

Comments
 (0)