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: pd.concat on axis "index" has unexpected result concatenating Series and DataFrame with same (column) name #56257

Closed
2 of 3 tasks
mosc9575 opened this issue Nov 30, 2023 · 4 comments · Fixed by #60983
Closed
2 of 3 tasks
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@mosc9575
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
print(pd.__version__) >>> '2.1.3'

df = pd.DataFrame(1, index=[0], columns=["test"])
s = pd.Series(1, index=[1], name="test")

pd.concat([df, s], axis="index")

>>> test    0
0   1.0  NaN
1   NaN  1.0

print(pd.concat([df, s.to_frame()], axis="index"))

>>> test
0   1
1   1

Issue Description

Concatenating a DataFrame and a Series using axis="index" results in a new DataFrame with two columns, even if the column name is equal to the name of the series. One column is named "0".

Converting the Series to a DataFrame before calling pd.concat results in a DataFrame with only on column.

Expected Behavior

I expected, that there is no difference in the behavior and the result between these two examples. To me the DataFrame with two columns is not intuitive.

Installed Versions

INSTALLED VERSIONS

commit : 2a953cf
python : 3.10.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.0-21-amd64
Version : #1 SMP Debian 4.19.249-2 (2022-06-30)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.3
numpy : 1.26.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.6.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader : None
bs4 : 4.11.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.7
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@mosc9575 mosc9575 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 30, 2023
@rob-sil
Copy link
Contributor

rob-sil commented Dec 6, 2023

Looks like the same issue as #15047. The code in question hasn't changed since the original implementation of mixed Series/DataFrame concatenations ten years ago (c6b21b4, #2385).

@alain-khalil
Copy link

Hello Moritz,
I fixed the issue. I did the tests locally and they work fine as you wish. I create a PR.
Please let me know if further clarification is required from my side.
Regards

@mosc9575
Copy link
Contributor Author

mosc9575 commented Dec 8, 2023

@alain-khalil Thanks for you work. This looks good to me. Will this be part of 2.1.4 or 2.2.0 release?

@Anurag-Varma
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants