Skip to content

Fix regression in loc setitem raising KeyError when enlarging df with multiindex #39161

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
Jan 15, 2021
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrameGroupBy.diff` raising for ``int8`` and ``int16`` columns (:issue:`39050`)
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
- Fixed regression in :meth:`DataFrame.groupby` when aggregating an :class:`ExtensionDType` that could fail for non-numeric values (:issue:`38980`)
-
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``KeyError`` with :class:`MultiIndex` and list-like columns indexer enlarging :class:`DataFrame` (:issue:`39147`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None):
if self.ndim != 2:
return

if isinstance(key, tuple) and not isinstance(self.obj.index, ABCMultiIndex):
if isinstance(key, tuple) and (
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we need len(key) as a condition?

Copy link
Contributor

Choose a reason for hiding this comment

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

wow this is a specific condition (i know i asked you to update). umm anyway we can be more general here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, was not happy with that either. This is better and does the job now.

isinstance(key[0], slice) or not isinstance(self.obj.index, ABCMultiIndex)
):
# key may be a tuple if we are .loc
# if index is not a MultiIndex, set key to column part
Copy link
Member

Choose a reason for hiding this comment

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

Thanks @phofl for the PR. does this comment need updating?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx

key = key[column_axis]
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 @@ -308,6 +308,18 @@ def test_multiindex_one_dimensional_tuple_columns(self, indexer):
expected = DataFrame([0, 2], index=mi)
tm.assert_frame_equal(obj, expected)

def test_multiindex_setitem_columns_enlarging(self):
# GH#39147
mi = MultiIndex.from_tuples([(1, 2), (3, 4)])
df = DataFrame([[1, 2], [3, 4]], index=mi, columns=["a", "b"])
df.loc[:, ["c", "d"]] = None
expected = DataFrame(
[[1, 2, None, None], [3, 4, None, None]],
index=mi,
columns=["a", "b", "c", "d"],
)
tm.assert_frame_equal(df, expected)


@pytest.mark.parametrize(
"indexer, pos",
Expand Down