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

Open
3 tasks done
davetapley opened this issue Nov 26, 2024 · 2 comments · May be fixed by #60437
Open
3 tasks done

BUG: combine_first reorders columns #60427

davetapley opened this issue Nov 26, 2024 · 2 comments · May be fixed by #60437
Assignees
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@davetapley
Copy link
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • 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.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.0+
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Wed Oct 4 23:55:28 PDT 2023; root:xnu-8020.240.18.704.15~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.1.3
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@davetapley davetapley added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 26, 2024
@U-S-jun
Copy link
Contributor

U-S-jun commented Nov 28, 2024

take

@U-S-jun U-S-jun linked a pull request Nov 28, 2024 that will close this issue
5 tasks
@rhshadrach
Copy link
Member

Thanks for the report, agreed this method should preserve column order. I am comfortable with calling this a bug.

@rhshadrach rhshadrach added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants