Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame({"B": [1, 2, 3], "A": [4, 5, 6]}, index=["a", "b", "c"])
print(df) # B first, then A
print()
df_ = pd.DataFrame({"A": [7]}, index=["b"])
print(df.combine_first(df_)) # A first, then B
print()
print(df_.combine_first(df)) # A first, then B
print()
print(df_.combine_first(df)[df.columns]) # Workaround
Issue Description
I wouldn't expect combine_first to reorder the columns alphabetically, but it does.
Bug might be a stretch, but it's certainly unexpected and awkward.
Expected Behavior
Preserve the column order, as show in # Workaround.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
I wouldn't expect
combine_first
to reorder the columns alphabetically, but it does.Bug might be a stretch, but it's certainly unexpected and awkward.
Expected Behavior
Preserve the column order, as show in
# Workaround
.Installed Versions