From 9034b1ba147729fc92917533c54c75e8d1179aa3 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Thu, 14 Jul 2022 06:44:52 -0700 Subject: [PATCH] Returns DataFrame When Concating Along Axis 1 (#11263) fixes #11244 When concatenating along axis 1, pandas always return a dataframe even though there are only 1 column involved. This PR conforms cuDF to that behavior. Authors: - Michael Wang (https://github.com/isVoid) Approvers: - GALI PREM SAGAR (https://github.com/galipremsagar) URL: https://github.com/rapidsai/cudf/pull/11263 --- python/cudf/cudf/core/reshape.py | 9 ++++++++- python/cudf/cudf/tests/test_concat.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/core/reshape.py b/python/cudf/cudf/core/reshape.py index 66f511a4324..3b462d7c6ac 100644 --- a/python/cudf/cudf/core/reshape.py +++ b/python/cudf/cudf/core/reshape.py @@ -266,7 +266,14 @@ def concat(objs, axis=0, join="outer", ignore_index=False, sort=None): index=cudf.RangeIndex(len(obj)), ) else: - result = obj.copy() + if axis == 0: + result = obj.copy() + else: + data = obj._data.copy(deep=True) + if isinstance(obj, cudf.Series) and obj.name is None: + # If the Series has no name, pandas renames it to 0. + data[0] = data.pop(None) + result = cudf.DataFrame._from_data(data) return result.sort_index(axis=axis) if sort else result diff --git a/python/cudf/cudf/tests/test_concat.py b/python/cudf/cudf/tests/test_concat.py index 70631ddb346..5094d938ea1 100644 --- a/python/cudf/cudf/tests/test_concat.py +++ b/python/cudf/cudf/tests/test_concat.py @@ -384,6 +384,16 @@ def test_pandas_concat_compatibility_axis1_eq_index(): ) +@pytest.mark.parametrize("name", [None, "a"]) +def test_pandas_concat_compatibility_axis1_single_column(name): + # Pandas renames series name `None` to 0 + # and preserves anything else + s = gd.Series([1, 2, 3], name=name) + got = gd.concat([s], axis=1) + expected = pd.concat([s.to_pandas()], axis=1) + assert_eq(expected, got) + + def test_concat_duplicate_columns(): cdf = gd.DataFrame( {