diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index b29089cb81a..8442cf05f01 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -405,12 +405,12 @@ def _setitem_tuple_arg(self, key, value): value = as_column(value, length=length) new_col = cudf.Series(value, index=idx) - if not self._frame.empty: + if len(self._frame.index) != 0: new_col = new_col._align_to_index( self._frame.index, how="right" ) - if self._frame.empty: + if len(self._frame.index) == 0: self._frame.index = ( idx if idx is not None else cudf.RangeIndex(len(new_col)) ) diff --git a/python/cudf/cudf/tests/test_indexing.py b/python/cudf/cudf/tests/test_indexing.py index 5f5c4579e01..f49b9b02076 100644 --- a/python/cudf/cudf/tests/test_indexing.py +++ b/python/cudf/cudf/tests/test_indexing.py @@ -2255,3 +2255,12 @@ def test_scalar_loc_row_categoricalindex(): result = df.loc["a"] expected = df.to_pandas().loc["a"] assert_eq(result, expected) + + +def test_loc_setitem_empty_dataframe(): + pdf = pd.DataFrame(index=["index_1", "index_2", "index_3"]) + gdf = cudf.from_pandas(pdf) + pdf.loc[["index_1"], "new_col"] = "A" + gdf.loc[["index_1"], "new_col"] = "A" + + assert_eq(pdf, gdf)