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

Fix stubname equal suffix in wide to long function #57868

Open
wants to merge 1 commit into
base: main
Choose a base branch
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
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
2 participants