Skip to content

BUG-Fix: AssertionError when slicing MultiIndex and setting value of … #35018

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4947,6 +4947,7 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind)

# return a slice

if not is_scalar(start_slice):
raise AssertionError("Start slice bound is non-scalar")
if not is_scalar(end_slice):
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2928,12 +2928,8 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
stop = len(level_index) - 1
step = key.step
except KeyError:

# we have a partial slice (like looking up a partial date
# string)
start = stop = level_index.slice_indexer(
key.start, key.stop, key.step, kind="loc"
)
start = key.start
stop = key.stop
step = start.step

if isinstance(start, slice) or isinstance(stop, slice):
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,22 @@ def test_loc_with_period_index_indexer():
tm.assert_frame_equal(df, df.loc[list(idx)])
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
tm.assert_frame_equal(df, df.loc[list(idx)])


def test_loc_setting_value_at_multiindex():
# GH 34870
arrays = [
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
array = [1, 1, 1, 1, 1, 1, 1, 1]
answer = [1, 1, 100, 100, 100, 100, 1, 1]

tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])

result = pd.Series(array, index=index)
expected = pd.Series(answer, index=index)

result.loc[("baz", "one"):("foo", "two")] = 100
tm.assert_series_equal(result, expected)