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

Push DeviceScalar construction into cython for list.contains #7864

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion python/cudf/cudf/_lib/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def extract_element(Column col, size_type index):
return result


def contains_scalar(Column col, DeviceScalar search_key):
def contains_scalar(Column col, object py_search_key):

cdef DeviceScalar search_key = py_search_key.device_value

cdef shared_ptr[lists_column_view] list_view = (
make_shared[lists_column_view](col.view())
)
Expand Down
3 changes: 2 additions & 1 deletion python/cudf/cudf/core/column/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ def contains(self, search_key):
Series([False, True, True])
dtype: bool
"""
search_key = cudf.Scalar(search_key)
try:
res = self._return_or_inplace(
contains_scalar(self._column, search_key.device_value)
contains_scalar(self._column, search_key)
)
except RuntimeError as e:
if (
Expand Down
19 changes: 18 additions & 1 deletion python/cudf/cudf/tests/test_contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cudf import Series
from cudf.core.index import RangeIndex, as_index
from cudf.tests.utils import assert_eq

from cudf.utils.dtypes import SCALAR_TYPES

def cudf_date_series(start, stop, freq):
return Series(pd.date_range(start, stop, freq=freq, name="times"))
Expand Down Expand Up @@ -72,3 +72,20 @@ def test_index_contains(values, item, expected):
def test_rangeindex_contains():
assert_eq(True, 9 in RangeIndex(start=0, stop=10, name="Index"))
assert_eq(False, 10 in RangeIndex(start=0, stop=10, name="Index"))

@pytest.mark.parametrize("dtype", SCALAR_TYPES)
kkraus14 marked this conversation as resolved.
Show resolved Hide resolved
def test_lists_contains(dtype):
dtype = np.dtype(dtype)
inner_data = np.array([1, 2, 3], dtype=dtype)
data = Series([inner_data])

if dtype.kind in "mM":
unit, _ = np.datetime_data(dtype)
contained_scalar = inner_data.dtype.type(2, unit)
not_contained_scalar = inner_data.dtype.type(42, unit)
else:
contained_scalar = inner_data.dtype.type(2)
not_contained_scalar = inner_data.dtype.type(42)

assert data.list.contains(contained_scalar)[0]
assert not data.list.contains(not_contained_scalar)[0]
8 changes: 7 additions & 1 deletion python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@
STRING_TYPES = {"object"}
BOOL_TYPES = {"bool"}
ALL_TYPES = NUMERIC_TYPES | DATETIME_TYPES | TIMEDELTA_TYPES | OTHER_TYPES

SCALAR_TYPES = (
NUMERIC_TYPES
| BOOL_TYPES
| STRING_TYPES
| TIMEDELTA_TYPES
| DATETIME_TYPES
)

def np_to_pa_dtype(dtype):
"""Util to convert numpy dtype to PyArrow dtype.
Expand Down