Skip to content

Commit

Permalink
Handle cupy array in Dataframe.__setitem__ (#7340)
Browse files Browse the repository at this point in the history
Fixes: #7337 

This PR introduces changes to handle `cupy` array in `DataFrame.__setitem__`.

Authors:
  - GALI PREM SAGAR (@galipremsagar)

Approvers:
  - Ashwin Srinath (@shwina)

URL: #7340
  • Loading branch information
galipremsagar authored Feb 8, 2021
1 parent eced957 commit ac02ae3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 5 additions & 17 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from cudf.utils import applyutils, docutils, ioutils, queryutils, utils
from cudf.utils.docutils import copy_docstring
from cudf.utils.dtypes import (
can_convert_to_column,
cudf_dtype_from_pydata_dtype,
find_common_type,
is_categorical_dtype,
Expand Down Expand Up @@ -683,20 +684,9 @@ def __getitem__(self, arg):
elif isinstance(arg, slice):
return self._slice(arg)

elif isinstance(
arg,
(
list,
cupy.ndarray,
np.ndarray,
pd.Series,
Series,
Index,
pd.Index,
),
):
elif can_convert_to_column(arg):
mask = arg
if isinstance(mask, list):
if is_list_like(mask):
mask = pd.Series(mask)
if mask.dtype == "bool":
return self._apply_boolean_mask(mask)
Expand Down Expand Up @@ -776,11 +766,9 @@ def __setitem__(self, arg, value):
# pandas raises key error here
self.insert(len(self._data), arg, value)

elif isinstance(
arg, (list, np.ndarray, pd.Series, Series, Index, pd.Index)
):
elif can_convert_to_column(arg):
mask = arg
if isinstance(mask, list):
if is_list_like(mask):
mask = np.array(mask)

if mask.dtype == "bool":
Expand Down
12 changes: 12 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8315,3 +8315,15 @@ def test_dataframe_roundtrip_arrow_struct_dtype(gdf):
expected = gd.DataFrame.from_arrow(table)

assert_eq(gdf, expected)


def test_dataframe_setitem_cupy_array():
np.random.seed(0)
pdf = pd.DataFrame(np.random.randn(10, 2))
gdf = gd.from_pandas(pdf)

gpu_array = cupy.array([True, False] * 5)
pdf[gpu_array.get()] = 1.5
gdf[gpu_array] = 1.5

assert_eq(pdf, gdf)

0 comments on commit ac02ae3

Please sign in to comment.