Skip to content

Commit

Permalink
BUG: Fix strange behaviour of Series.iloc on MultiIndex Series (#17148)
Browse files Browse the repository at this point in the history
  • Loading branch information
ante012 committed Aug 19, 2017
1 parent 95f4f7d commit 19ffe3d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ Indexing
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
- Bug ``Series`` with ``MultiIndex`` gives surprising ``.iloc`` results when an integer indexer is used (:issue: `17148`)

I/O
^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import textwrap
import warnings
import numpy as np
import numbers
from pandas.compat import range, zip
import pandas.compat as compat
from pandas.core.dtypes.generic import ABCDataFrame, ABCPanel, ABCSeries
Expand Down Expand Up @@ -146,7 +147,8 @@ def _get_setitem_indexer(self, key):
return self._convert_tuple(key, is_setter=True)

axis = self.obj._get_axis(0)
if isinstance(axis, MultiIndex):

if isinstance(axis, MultiIndex) and not isinstance(key, int):
try:
return axis.get_loc(key)
except Exception:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ def test_iloc_setitem(self):
expected = Series([0, 1, 0], index=[4, 5, 6])
tm.assert_series_equal(s, expected)

df = pd.DataFrame(data=[[1, 2, 5], [1, 3, 6]], columns=['i', 'j', 'k'])
df.set_index(['i', 'j'], inplace=True)
s = df.k.copy()
s.iloc[1] = 1
df.values[1][0] = 1
expected = df.k
tm.assert_series_equal(s, expected)

def test_iloc_setitem_list(self):

# setitem with an iloc list
Expand Down

0 comments on commit 19ffe3d

Please sign in to comment.