-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Fix strange behaviour of Series.iloc on MultiIndex Series (#17148) #17291
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,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 is_integer(key): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
try: | ||
return axis.get_loc(key) | ||
except Exception: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,6 +269,40 @@ def test_iloc_setitem(self): | |
expected = Series([0, 1, 0], index=[4, 5, 6]) | ||
tm.assert_series_equal(s, expected) | ||
|
||
def test_iloc_setitem_int_multiindex_series(self): | ||
# GH17148 | ||
def check_scenario(data, indexes, values, expected_k): | ||
df = pd.DataFrame( | ||
data=data, | ||
columns=['i', 'j', 'k']) | ||
df.set_index(['i', 'j'], inplace=True) | ||
|
||
series = df.k.copy() | ||
for i, v in zip(indexes, values): | ||
series.iloc[i] += v | ||
|
||
df.k = expected_k | ||
expected = df.k.copy() | ||
tm.assert_series_equal(series, expected) | ||
|
||
check_scenario( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parametrize this test |
||
data=[[1, 22, 5], [1, 33, 6]], | ||
indexes=[0, -1, 1], | ||
values=[2, 3, 1], | ||
expected_k=[7, 10]) | ||
|
||
check_scenario( | ||
data=[[1, 3, 7], [2, 4, 8]], | ||
indexes=[0, -1, 1], | ||
values=[1, 1, 10], | ||
expected_k=[8, 19]) | ||
|
||
check_scenario( | ||
data=[[1, 11, 4], [2, 22, 5], [3, 33, 6]], | ||
indexes=[0, -1, 1], | ||
values=[4, 7, 10], | ||
expected_k=[8, 15, 13]) | ||
|
||
def test_iloc_setitem_list(self): | ||
|
||
# setitem with an iloc list | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its a bug in
.iloc
not restricted to Series