Skip to content

Fix stubname equal suffix in wide to long function #57868

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

tqa236
Copy link
Contributor

@tqa236 tqa236 commented Mar 16, 2024

As pandas allows an index in a multiindex and a column to have the same name, I think it's reasonable to allow stubname equal suffix.

However, I think the root cause is in the melt function. It's stated in the docstring that

    value_name : scalar, default 'value'
        Name to use for the 'value' column, can't be an existing column label.

But if value_name == var_name, illogical result is returned. Example:

df = pd.DataFrame(
    {
        "A": {0: "a", 1: "b", 2: "c"},
        "B": {0: 1, 1: 3, 2: 5},
        "C": {0: 2, 1: 4, 2: 6},
    }
)
pd.melt(
    df,
    id_vars=["A"],
    value_vars=["B"],
    var_name="myVarname",
    value_name="myVarname",
)

Result:

image

I think it's more reasonable to also raise an error if value_name == var_name in melt

@tqa236 tqa236 force-pushed the fix-wide-to-long branch from f17bd8e to ddad4a9 Compare March 19, 2024 20:04
@tqa236 tqa236 force-pushed the fix-wide-to-long branch from ddad4a9 to fe29c82 Compare April 4, 2024 12:34
@tqa236 tqa236 force-pushed the fix-wide-to-long branch from fe29c82 to 6c00603 Compare April 9, 2024 21:18
@tqa236
Copy link
Contributor Author

tqa236 commented Apr 9, 2024

Hi @mroeschke, can you take a look at this PR, please? It should be ready for review.

@mroeschke mroeschke added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Apr 23, 2024
Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Thanks @tqa236, and sorry for the delay. Not sure if two branches got mixed, seems like half of the changes here are unrelated to the topic of this PR.

@tqa236 tqa236 force-pushed the fix-wide-to-long branch from 5d0f39d to 3106d68 Compare May 23, 2025 05:41
@tqa236
Copy link
Contributor Author

tqa236 commented May 23, 2025

hi @datapythonista, thank you for the review. I also force pushed to the branch because I noticed that you also didn't make any comment on the actual content of the PR. Now this PR should be ready for review.

@datapythonista
Copy link
Member

Thanks for the update here @tqa236. The fix you implemented seems a hack, not the proper fix. You should identify what's causing the exception, which seem kind of unrelated, and fix that. Please let me know if you want to have a look more in detail to the actual problem, otherwise we can close this PR.

@tqa236
Copy link
Contributor Author

tqa236 commented May 26, 2025

thank you for taking a look @datapythonista, unfortunately it's been a year and the context of this issue is no longer fresh in my mind. Let's close this PR.

@tqa236 tqa236 closed this May 26, 2025
@tqa236 tqa236 deleted the fix-wide-to-long branch May 26, 2025 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: pd.wide_to_long can't handle stubname equal to suffix
3 participants