Skip to content

Commit

Permalink
empty df
Browse files Browse the repository at this point in the history
  • Loading branch information
isVoid committed Apr 20, 2021
1 parent 7ef62bb commit 148f0f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 14 additions & 5 deletions python/cudf/cudf/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,27 @@ def _setitem_tuple_arg(self, key, value):
try:
columns = self._get_column_selection(key[1])
except KeyError:
if isinstance(key[0], slice):
if not self._df.empty and isinstance(key[0], slice):
pos_range = get_label_range_or_mask(
self._df.index, key[0].start, key[0].stop, key[0].step
)
idx = self._df.index[pos_range]
elif self._df.empty and isinstance(key[0], slice):
idx = None
else:
idx = cudf.Index(key[0])
if is_scalar(value):
value = as_column(value, length=len(idx))
new_col = cudf.Series(value, index=idx)._align_to_index(
self._df.index, how="right"
)
length = len(idx) if idx is not None else 1
value = as_column(value, length=length)

new_col = cudf.Series(value, index=idx)
if not self._df.empty:
new_col = new_col._align_to_index(self._df.index, how="right")

if self._df.empty:
self._df.index = (
idx if idx is not None else cudf.RangeIndex(len(new_col))
)
self._df._data.insert(key[1], new_col)
else:
for col in columns:
Expand Down
15 changes: 15 additions & 0 deletions python/cudf/cudf/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,21 @@ def test_dataframe_setitem_loc(key, value, pdf_gdf):
assert_eq(pdf, gdf)


@pytest.mark.parametrize(
"key, value",
[
(("one", "a"), 5),
((slice(None), "a"), range(3)),
((slice(None), "a"), [3, 2, 1]),
],
)
def test_dataframe_setitem_loc_empty_df(key, value):
pdf, gdf = pd.DataFrame(), cudf.DataFrame()
pdf.loc[key] = value
gdf.loc[key] = value
assert_eq(pdf, gdf, check_dtype=False)


@pytest.mark.parametrize(
"key,value",
[
Expand Down

0 comments on commit 148f0f3

Please sign in to comment.