-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Let melt
name multiple variable columns for labels from a MultiIndex
#58088
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: Let melt
name multiple variable columns for labels from a MultiIndex
#58088
Conversation
Thanks for the PR! Sorry this hasn't been reviewed yet. |
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
Hi! Just checking in as this issue has been affecting my code. I wondered if anything needed to be done or if this will be merged in a later release? |
tm.assert_frame_equal(df.melt(var_name=["first", "second"]), expected) | ||
tm.assert_frame_equal(df.melt(var_name=["first"]), expected[["first", "value"]]) | ||
|
||
with pytest.raises( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make this a separate test?
pandas/core/reshape/melt.py
Outdated
@@ -217,7 +218,15 @@ def melt( | |||
frame.columns.name if frame.columns.name is not None else "variable" | |||
] | |||
elif is_list_like(var_name): | |||
raise ValueError(f"{var_name=} must be a scalar.") | |||
if isinstance(frame.columns, MultiIndex): | |||
var_name = list(var_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the list
cast needed here? it should be Sized
to compute len
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var_name
will be iterated on twice, so I think there's a pathological case where you could have a single-use object that has __len__
and __iter__
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true. Could you use is_iterator
to check for the single use object and cast to list
if so?
doc/source/whatsnew/v2.2.2.rst
Outdated
@@ -15,6 +15,7 @@ Fixed regressions | |||
~~~~~~~~~~~~~~~~~ | |||
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pandas nullable on with missing values (:issue:`56702`) | |||
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pyarrow nullable on with missing values (:issue:`57664`) | |||
- :meth:`DataFrame.melt` would not accept multiple names in ``var_name`` when the columns were a :class:`MultiIndex` (:issue:`58033`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this to v3.0.0rst
?
Thanks @rob-sil |
Thanks for moving this forward! |
my code works in jupyter notebook with 'var_names' parameter but not in py file, it gives the same error when running in py file. Interesting. |
melt
name the columns for the labels of aMultiIndex
#58033doc/source/whatsnew/v2.2.2.rst
file if fixing a bug or adding a new feature.If there are fewer names provided in
var_name
than levels in the columnsMultiIndex
, thenmelt
will just take the first several. I personally find this a bit strange, but it keeps with the previous behavior ofmelt
with aMultiIndex
where you could pass it a single name and it would only use one level of theMultiIndex
.