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 2 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
17 changes: 11 additions & 6 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2929,12 +2929,17 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
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"
)
step = start.step
if isinstance(key.start, tuple) or isinstance(key.stop, tuple):
return convert_indexer(key.start, key.stop, key.step)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than returning should not just do

start = key.start
stop = key.stop

and fall thru?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand what you mean here. Could you please explain?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add those 2 lines, then you don't need this if else.


else:
# 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"
)
step = start.step

if isinstance(start, slice) or isinstance(stop, slice):
# we have a slice for start and/or stop
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"])

dt1 = pd.Series(array, index=index)
dt2 = pd.Series(answer, index=index)

dt1.loc[("baz", "one"):("foo", "two")] = 100
tm.assert_series_equal(dt1, dt2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u do
result =
expected =