Skip to content

BUG: add back check for MultiIndex case and take_split_path #27855

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

Merged
merged 4 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,19 @@ def _setitem_with_indexer(self, indexer, value):
val = list(value.values()) if isinstance(value, dict) else value
take_split_path = not blk._can_hold_element(val)

if isinstance(indexer, tuple) and len(indexer) == len(self.obj.axes):

for i, ax in zip(indexer, self.obj.axes):

# if we have any multi-indexes that have non-trivial slices
# (not null slices) then we must take the split path, xref
# GH 10360
if isinstance(ax, MultiIndex) and not (
is_integer(i) or com.is_null_slice(i)
):
take_split_path = True
break

if isinstance(indexer, tuple):
nindexer = []
for i, idx in enumerate(indexer):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,15 @@ def test_loc_getitem_lowerdim_corner(multiindex_dataframe_random_data):
expected = 0
result = df.sort_index().loc[("bar", "three"), "B"]
assert result == expected


def test_loc_setitem_object_array():
# case from https://github.com/pandas-dev/pandas/issues/27841
df = DataFrame(
"string",
index=list("abcd"),
columns=MultiIndex.from_product([["Main"], ("another", "one")]),
)
df["labels"] = "a"
df.loc[:, "labels"] = df.index
tm.assert_numpy_array_equal(np.asarray(df["labels"]), np.asarray(df.index))