diff --git a/python/cudf/cudf/core/column_accessor.py b/python/cudf/cudf/core/column_accessor.py index c2ea9d756f7..67976ac27d4 100644 --- a/python/cudf/cudf/core/column_accessor.py +++ b/python/cudf/cudf/core/column_accessor.py @@ -1,4 +1,4 @@ -# Copyright (c) 2021, NVIDIA CORPORATION. +# Copyright (c) 2021-2022, NVIDIA CORPORATION. from __future__ import annotations @@ -523,14 +523,19 @@ def rename_column(x): raise IndexError( f"Too many levels: Index has only 1 level, not {level+1}" ) + if isinstance(mapper, Mapping): - new_names = ( + new_col_names = [ mapper.get(col_name, col_name) for col_name in self.keys() - ) + ] else: - new_names = (mapper(col_name) for col_name in self.keys()) + new_col_names = [mapper(col_name) for col_name in self.keys()] + + if len(new_col_names) != len(set(new_col_names)): + raise ValueError("Duplicate column names are not allowed") + ca = ColumnAccessor( - dict(zip(new_names, self.values())), + dict(zip(new_col_names, self.values())), level_names=self.level_names, multiindex=self.multiindex, ) diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index fa7680df8a5..ec2e1616760 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -9083,3 +9083,11 @@ def test_dataframe_assign_cp_np_array(): gdf[[f"f_{i}" for i in range(n)]] = cp_ndarray assert_eq(pdf, gdf) + + +def test_dataframe_rename_duplicate_column(): + gdf = cudf.DataFrame({"a": [1, 2, 3], "b": [3, 4, 5]}) + with pytest.raises( + ValueError, match="Duplicate column names are not allowed" + ): + gdf.rename(columns={"a": "b"}, inplace=True)