From c786258c953dfbeeb33e89dc2fc71688eb1ec54a Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Tue, 1 Mar 2022 13:34:38 -0800 Subject: [PATCH 1/2] fix issue with scalar broadcasting --- python/cudf/cudf/core/dataframe.py | 5 ++++- python/cudf/cudf/tests/test_setitem.py | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 287fd3796f4..2915c63b938 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -1107,6 +1107,7 @@ def __getitem__(self, arg): @annotate("DATAFRAME_SETITEM", color="blue", domain="cudf_python") def __setitem__(self, arg, value): """Add/set column by *arg or DataFrame*""" + # import pdb;pdb.set_trace() if isinstance(arg, DataFrame): # not handling set_item where arg = df & value = df if isinstance(value, DataFrame): @@ -1161,7 +1162,9 @@ def __setitem__(self, arg, value): allow_non_unique=True, ) if is_scalar(value): - self._data[arg][:] = value + self._data[arg] = utils.scalar_broadcast_to( + value, len(self) + ) else: value = as_column(value) self._data[arg] = value diff --git a/python/cudf/cudf/tests/test_setitem.py b/python/cudf/cudf/tests/test_setitem.py index 1fce7853fdf..fd3f2732556 100644 --- a/python/cudf/cudf/tests/test_setitem.py +++ b/python/cudf/cudf/tests/test_setitem.py @@ -33,9 +33,14 @@ def test_dataframe_setitem_scaler_bool(): assert_eq(df, gdf) -@pytest.mark.parametrize("df", [pd.DataFrame({"a": [1, 2, 3]})]) +@pytest.mark.parametrize( + "df", + [pd.DataFrame({"a": [1, 2, 3]}), pd.DataFrame({"a": ["x", "y", "z"]})], +) @pytest.mark.parametrize("arg", [["a"], "a", "b"]) -@pytest.mark.parametrize("value", [-10, pd.DataFrame({"a": [-1, -2, -3]})]) +@pytest.mark.parametrize( + "value", [-10, pd.DataFrame({"a": [-1, -2, -3]}), "abc"] +) def test_dataframe_setitem_columns(df, arg, value): gdf = cudf.from_pandas(df) cudf_replace_value = value From b9c90919d021f7b7bfa662de5166ed6d064e32a1 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Wed, 2 Mar 2022 13:46:33 -0600 Subject: [PATCH 2/2] Update python/cudf/cudf/core/dataframe.py Co-authored-by: Bradley Dice --- python/cudf/cudf/core/dataframe.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 2915c63b938..e667c4a1e58 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -1107,7 +1107,6 @@ def __getitem__(self, arg): @annotate("DATAFRAME_SETITEM", color="blue", domain="cudf_python") def __setitem__(self, arg, value): """Add/set column by *arg or DataFrame*""" - # import pdb;pdb.set_trace() if isinstance(arg, DataFrame): # not handling set_item where arg = df & value = df if isinstance(value, DataFrame):