Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cupy array in Dataframe.__setitem__ #7340

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)