-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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: Concatenating dataframe and series with ignore_index = True
drops the series name
#60723
Comments
It's true that you can not have the series index without converting into a Dataframe first. To merge a Series with a Dataframe without changing the Series into a Dataframe, while using ignore_index=True, you can convert the Series to a DataFrame temporarily within the pd.concat function. Here's how you can do it: `import pandas as pd df = pd.DataFrame({'a': [0, 1], 'b': [2, 3]}) Concatenating DataFrame and Series with ignore_index=Trueresult = pd.concat([df, s.to_frame().T], ignore_index=True) print(result) |
Thanks for the report, agreed with the expectation to maintain the Series name here. Further investigations and PRs to fix are welcome! |
take |
Hi @rhshadrach , i am trying this bug and there is a wrong test case which has the below code and outputs. Test case: Python code for the Test case: import numpy as np
import pandas as pd
# Create date index
index = pd.date_range("01-Jan-2013", periods=10, freq="h")
# Create array
arr = np.arange(10, dtype="int64")
# Create Series
s1 = pd.Series(arr, index=index)
s2 = pd.Series(arr, index=index)
# Create DataFrame from array
df = pd.DataFrame(arr.reshape(-1, 1), index=index)
# Concatenate along columns
result = pd.concat([s1, df, s2, s2, s1], axis=1)
# Expected DataFrame with correct column naming
expected = pd.DataFrame(
np.repeat(arr, 5).reshape(-1, 5), index=index, columns=[0, 0, 1, 2, 3]
)
# Print results
print("Result with to_frame:")
print(pd.concat([s1.to_frame(), df, s2.to_frame(), s2.to_frame(), s1.to_frame()], axis=1))
print("Result after my code changes to the bug:")
print(result)
print("Expected from test case:")
print(expected) Outputs:
Can you say if result with to_frame() is correct in the I am thinking that the test case expected output is wrong here ! If that's the case, then i have to change the old test case from 10 years back and change the expected output |
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
Issue Description
When I concatenate a dataframe with a series and pass
ignore_index=True
, the series' name does not show up in the resulting dataframe. This is surprising, because the documentation forignore_index
says, "Note the index values on the other axes are still respected in the join."This seems similar to #56257. That concerned the case in which the name of the series is the same as the name of one of the columns of the dataframe but did not involve
ignore_index
. That issue has a stale PR (#56362) that looks like it might fix this one, too.Expected Behavior
Doing
pd.concat([df, s], ignore_index=True)
should preserve the name of the series.Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.2
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 2.2.3
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
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.1
qtpy : None
pyqt5 : None
None
The text was updated successfully, but these errors were encountered: