Skip to content

Commit

Permalink
Handle empty dataframe object with index present in setitem of loc (#…
Browse files Browse the repository at this point in the history
…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: #15752
  • Loading branch information
galipremsagar authored May 15, 2024
1 parent 4a6d13f commit 04d247c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
Expand Down
9 changes: 9 additions & 0 deletions python/cudf/cudf/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 04d247c

Please sign in to comment.