-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: fix combine_first reorders columns #60791
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
Conversation
d3e867e
to
aefa296
Compare
pandas/core/frame.py
Outdated
@@ -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) |
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.
Why does this need to be done here as opposed to where it was before?
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.
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.
Thanks @yuanx749 |
I changed an old test for the correct order of columns.
Note that the fix can also be done incombine
, if it is preferred to be fixed there, sincecombine_first
callscombine
.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 incombine
.doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.