Skip to content

ENH: allow for top and mid-level assignment to DataFrames with MultIndex columns #7475 #36755

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 2 commits into from

Conversation

iamlemec
Copy link
Contributor

This allows for one to assign to the top and mid-levels of DataFrame columns. To do so, it adds another fallback execution path to __setitem__. The logic is:

  • In __setitem__, if the other existing paths don't apply, there is a single key, and the columns are a MultiIndex, pass to _setitem_multilevel. Here, assign if the key exists in the columns. Otherwise, use the new function append_block to assign multiple columns at once.
  • In the existing _setitem_array, as a fallback, if the length of the assigning key equals the length of the top level of the value columns, recurse these back onto __setitem__. This allows for higher level list assignments.

I've also added some tests of the functionality in various cases. Any comments much appreciated.

Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

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

I don't have a comment on the changes themselves.

However, looking through the code there's two places where you added lines that aren't currently hit by tests (according to Codecov). We probably want to expand the test cases to include those

@iamlemec
Copy link
Contributor Author

Thanks for the comment @arw2019. Just added in some new tests for that exception path. I believe there is full coverage now. The only weird thing I found was that indexing/multiindex/test_insert.py expects assigning one column to a multiindex with insufficient key levels to work regardless, so I commented out what would have been a conflicting test on my side.

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.

@iamlemec
Copy link
Contributor Author

Sorry for the delay here, finally brought this up to date again. Had to change a few things to work well with the changes in #38459. Basically this is now a sub-path of _set_item_frame_value that kicks in when the key is not in the existing columns and when it is not a fully specified column.

@github-actions github-actions bot removed the Stale label Jan 23, 2021
@jreback
Copy link
Contributor

jreback commented Oct 4, 2021

closing as stale, if you want to continue working, please ping.

@jreback jreback closed this Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH/BUG: support multi-index slicing assignment expansions
3 participants