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: fix combine_first reorders columns #60791

Merged
merged 6 commits into from
Jan 27, 2025

Conversation

yuanx749
Copy link
Contributor

@yuanx749 yuanx749 commented Jan 26, 2025

I changed an old test for the correct order of columns.

Note that the fix can also be done in combine, if it is preferred to be fixed there, since combine_first calls combine.

With my previous fix, a test in "Future infer strings" failed, which is the corner case when self is an empty dataframe. I then made the fix in combine.

@@ -8672,6 +8672,9 @@ def combine(
"""
other_idxlen = len(other.index) # save for compare

# preserve column order
new_columns = self.columns.union(other.columns, sort=False)
Copy link
Member

Choose a reason for hiding this comment

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

Why does this need to be done here as opposed to where it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put it before this, other = self.align(other) because align can change the column order of other, as it sorts keys lexicographically.

But I think it is better to do it where it was before, after the early return. I've updated it in the new commit.

@mroeschke mroeschke added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Jan 27, 2025
@mroeschke mroeschke added this to the 3.0 milestone Jan 27, 2025
@mroeschke mroeschke merged commit e36b000 into pandas-dev:main Jan 27, 2025
41 of 42 checks passed
@mroeschke
Copy link
Member

Thanks @yuanx749

@yuanx749 yuanx749 deleted the combine_first branch January 28, 2025 07:13
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: combine_first reorders columns
2 participants