From 04d247c072b55ce8265d18c7e1a56e4abb31f6cf Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Wed, 15 May 2024 06:34:25 -0500 Subject: [PATCH] Handle empty dataframe object with index present in setitem of `loc` (#15752) Fixes: #15718 This PR fixes an issue with `loc` setitem where the dataframe is empty but has an index of length greater than 0. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Matthew Roeschke (https://github.com/mroeschke) URL: https://github.com/rapidsai/cudf/pull/15752 --- python/cudf/cudf/core/dataframe.py | 4 ++-- python/cudf/cudf/tests/test_indexing.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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)