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: Let melt name multiple variable columns for labels from a MultiIndex #58088

Merged
merged 4 commits into from
May 25, 2024

Conversation

rob-sil
Copy link
Contributor

@rob-sil rob-sil commented Mar 30, 2024

If there are fewer names provided in var_name than levels in the columns MultiIndex, then melt will just take the first several. I personally find this a bit strange, but it keeps with the previous behavior of melt with a MultiIndex where you could pass it a single name and it would only use one level of the MultiIndex.

@rob-sil rob-sil marked this pull request as ready for review March 30, 2024 23:09
@Aloqeely
Copy link
Member

Thanks for the PR! Sorry this hasn't been reviewed yet.
Generally LGTM. cc @mroeschke

Copy link
Contributor

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.

@github-actions github-actions bot added the Stale label May 21, 2024
@remi-pr
Copy link

remi-pr commented May 21, 2024

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(
Copy link
Member

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?

@@ -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)
Copy link
Member

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

Copy link
Contributor Author

@rob-sil rob-sil May 21, 2024

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

Copy link
Member

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?

@@ -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`)
Copy link
Member

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?

@mroeschke mroeschke added Reshaping Concat, Merge/Join, Stack/Unstack, Explode MultiIndex and removed Stale labels May 21, 2024
@rob-sil rob-sil requested a review from mroeschke May 25, 2024 18:36
@mroeschke mroeschke merged commit b162331 into pandas-dev:main May 25, 2024
47 checks passed
@mroeschke
Copy link
Member

Thanks @rob-sil

@remi-pr
Copy link

remi-pr commented May 26, 2024

Thanks for moving this forward!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: No longer can melt name the columns for the labels of a MultiIndex
4 participants