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

Conversation

phofl
Copy link
Member

@phofl phofl commented Jan 14, 2021

The previous fix only viewed row indexers not column indexers

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version labels Jan 14, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.1 milestone Jan 14, 2021
if isinstance(key, tuple) and not isinstance(self.obj.index, ABCMultiIndex):
if isinstance(key, tuple) and (
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

@@ -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.

@jreback jreback merged commit 49778b0 into pandas-dev:master Jan 15, 2021
@jreback
Copy link
Contributor

jreback commented Jan 15, 2021

thanks @phofl

@jreback
Copy link
Contributor

jreback commented Jan 15, 2021

@meeseeksdev backport 1.2.x

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Jan 15, 2021
simonjayhawkins pushed a commit that referenced this pull request Jan 15, 2021
…en enlarging df with multiindex (#39187)

Co-authored-by: patrick <[email protected]>
@phofl phofl deleted the 39147 branch January 15, 2021 16:04
luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REGR: Assigning multiple new columns with loc fails when index is a MultiIndex
3 participants