Skip to content

Commit bc24e84

Browse files
BUG: Fix concat DataFrame and Series with ignore_index=True (#60983)
* modified the files according to bug#60237 * Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> * moved test case to frame and serier folders * fix pyarrow import error * inconsistent issue fix * added test cases and fixed old pr test cases * added rst and small changes in tests file * fixed column name issue for column wise concat * fixed text case for concat * fix test cases issue * Trigger redeployment * fixed reviewed changes and added extra test cases * removed duplicate test case --------- Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
1 parent 7f58d74 commit bc24e84

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ Reshaping
788788
- Bug in :meth:`DataFrame.pivot_table` incorrectly subaggregating results when called without an ``index`` argument (:issue:`58722`)
789789
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
790790
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
791+
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
791792

792793
Sparse
793794
^^^^^^

pandas/core/reshape/concat.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -477,18 +477,23 @@ def _sanitize_mixed_ndim(
477477

478478
else:
479479
name = getattr(obj, "name", None)
480+
rename_columns = False
480481
if ignore_index or name is None:
481482
if axis == 1:
482483
# doing a row-wise concatenation so need everything
483484
# to line up
484-
name = 0
485+
if name is None:
486+
name = 0
487+
rename_columns = True
485488
else:
486489
# doing a column-wise concatenation so need series
487490
# to have unique names
488-
name = current_column
489-
current_column += 1
491+
if name is None:
492+
rename_columns = True
493+
name = current_column
494+
current_column += 1
490495
obj = sample._constructor(obj, copy=False)
491-
if isinstance(obj, ABCDataFrame):
496+
if isinstance(obj, ABCDataFrame) and rename_columns:
492497
obj.columns = range(name, name + 1, 1)
493498
else:
494499
obj = sample._constructor({name: obj}, copy=False)

pandas/tests/reshape/concat/test_concat.py

+60-2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ def test_concat_mixed_objs_index(self):
326326
def test_concat_mixed_objs_index_names(self):
327327
# Test row-wise concat for mixed series/frames with distinct names
328328
# GH2385, GH15047
329+
# GH #60723 & GH #56257 (Updated the test case,
330+
# as the above GH PR ones were incorrect)
329331

330332
index = date_range("01-Jan-2013", periods=10, freq="h")
331333
arr = np.arange(10, dtype="int64")
@@ -341,8 +343,11 @@ def test_concat_mixed_objs_index_names(self):
341343
result = concat([s1, df, s2])
342344
tm.assert_frame_equal(result, expected)
343345

344-
# Rename all series to 0 when ignore_index=True
345-
expected = DataFrame(np.tile(arr, 3).reshape(-1, 1), columns=[0])
346+
expected = DataFrame(
347+
np.kron(np.where(np.identity(3) == 1, 1, np.nan), arr).T,
348+
index=np.arange(30, dtype=np.int64),
349+
columns=["foo", 0, "bar"],
350+
)
346351
result = concat([s1, df, s2], ignore_index=True)
347352
tm.assert_frame_equal(result, expected)
348353

@@ -943,3 +948,56 @@ def test_concat_with_moot_ignore_index_and_keys():
943948
msg = f"Cannot set {ignore_index=} and specify keys. Either should be used."
944949
with pytest.raises(ValueError, match=msg):
945950
concat([df1, df2], keys=keys, ignore_index=ignore_index)
951+
952+
953+
@pytest.mark.parametrize(
954+
"inputs, ignore_index, axis, expected",
955+
[
956+
# Concatenating DataFrame and named Series without ignore_index
957+
(
958+
[DataFrame({"a": [0, 1], "b": [2, 3]}), Series([4, 5], name="c")],
959+
False,
960+
0,
961+
DataFrame(
962+
{
963+
"a": [0, 1, None, None],
964+
"b": [2, 3, None, None],
965+
"c": [None, None, 4, 5],
966+
},
967+
index=[0, 1, 0, 1],
968+
),
969+
),
970+
# Concatenating DataFrame and named Series with ignore_index
971+
(
972+
[DataFrame({"a": [0, 1], "b": [2, 3]}), Series([4, 5], name="c")],
973+
True,
974+
0,
975+
DataFrame(
976+
{
977+
"a": [0, 1, None, None],
978+
"b": [2, 3, None, None],
979+
"c": [None, None, 4, 5],
980+
},
981+
index=[0, 1, 2, 3],
982+
),
983+
),
984+
# Concatenating DataFrame and unnamed Series along columns
985+
(
986+
[DataFrame({"a": [0, 1], "b": [2, 3]}), Series([4, 5]), Series([4, 5])],
987+
False,
988+
1,
989+
DataFrame({"a": [0, 1], "b": [2, 3], 0: [4, 5], 1: [4, 5]}, index=[0, 1]),
990+
),
991+
# Concatenating DataFrame and unnamed Series along columns with ignore_index
992+
(
993+
[DataFrame({"a": [0, 1], "b": [2, 3]}), Series([4, 5]), Series([4, 5])],
994+
True,
995+
1,
996+
DataFrame({0: [0, 1], 1: [2, 3], 2: [4, 5], 3: [4, 5]}, index=[0, 1]),
997+
),
998+
],
999+
)
1000+
def test_concat_of_series_and_frame(inputs, ignore_index, axis, expected):
1001+
# GH #60723 and #56257
1002+
result = concat(inputs, ignore_index=ignore_index, axis=axis)
1003+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)