Skip to content
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

BUG: DataFrame.__setitem__ not raising ValueError when rhs is df and has wrong number of columns #39341

Merged
merged 2 commits into from
Jan 24, 2021

Conversation

phofl
Copy link
Member

@phofl phofl commented Jan 22, 2021

Have to check how many columns key references before raising (duplicates or MultiIndex may reference more than one column)

@phofl phofl added the Indexing Related to indexing on series/frames, not to indexes themselves label Jan 22, 2021
@jreback jreback added this to the 1.3 milestone Jan 22, 2021
# align right-hand-side columns if self.columns
# is multi-index and self[key] is a sub-frame
if isinstance(self.columns, MultiIndex) and key in self.columns:
if key in self.columns:
loc = self.columns.get_loc(key)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment: # align columns before L3259

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

# align right-hand-side columns if self.columns
# is multi-index and self[key] is a sub-frame
if isinstance(self.columns, MultiIndex):
if isinstance(loc, (slice, Series, np.ndarray, Index)):
Copy link
Contributor

Choose a reason for hiding this comment

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

can combine these conditions

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@jreback jreback merged commit caf81fa into pandas-dev:master Jan 24, 2021
@jreback
Copy link
Contributor

jreback commented Jan 24, 2021

thanks @phofl

@phofl phofl deleted the 38694 branch January 24, 2021 22:01
df = DataFrame([[1, 2, 3]], columns=["a", "b", "b"])
rhs = DataFrame([[10, 11, 12]], columns=["d", "e", "c"])
df[["a", "b"]] = rhs
expected = DataFrame([[10, 11, 12]], columns=["a", "b", "b"])
Copy link
Member

Choose a reason for hiding this comment

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

is this obvious? is rhs.columns irrelevant?

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean with obvious?

Yes columns are irrelevant with setitem

@@ -3223,7 +3223,7 @@ def _setitem_array(self, key, value):
self._check_setitem_copy()
self.iloc[indexer] = value
else:
if isinstance(value, DataFrame):
if isinstance(value, DataFrame) and self.columns.is_unique:
Copy link
Member

Choose a reason for hiding this comment

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

will there be a problem on L3230 if value.columns is not unique?

Copy link
Member Author

Choose a reason for hiding this comment

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

No but maybe at 3227

Copy link
Member Author

Choose a reason for hiding this comment

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

Depends of the key references a dup column

Copy link
Member Author

@phofl phofl Jan 25, 2021

Choose a reason for hiding this comment

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

let me rephrase this:
We don't get there, becasue 3227 would raise. If we would change the condition to recognize duplicates, we would get a problem in 3229, because the lenght of both lists would not match.
If we also fix this, we have to check that in case of a key which is duplicated in the columns, we select two columns and not only one in 3230 on the rhs

Edit: Similar problem if rhs would have duplicates.

Copy link
Member

Choose a reason for hiding this comment

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

yah the other case im now unsure of is not all(x in self.columns for x in key)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yikes, this has ugly side effects. We have to find a way to handle duplicates there instead of dispatching down :(

And improve the test coverage of these cases

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: surprising non-error when assigning a multi-column DataFrame to a single column
3 participants