Skip to content
Open
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
17 changes: 13 additions & 4 deletions python/cudf/cudf/core/accessors/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def __init__(self, parent: Series | Index):
)
super().__init__(parent=parent)

def _return_integer(self, new_col: ColumnBase) -> Series | Index:
if (
isinstance(self._column.dtype, pd.StringDtype)
and self._column.dtype.na_value is pd.NA
):
# Nullable string methods return pandas' nullable 64-bit integers.
new_col = new_col.astype(pd.Int64Dtype())
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return self._return_or_inplace(new_col)

def htoi(self) -> Series | Index:
"""
Returns integer value represented by each hex string.
Expand Down Expand Up @@ -201,7 +210,7 @@ def len(self) -> Series | Index:
3 <NA>
dtype: int32
"""
return self._return_or_inplace(self._column.count_characters())
return self._return_integer(self._column.count_characters())

def byte_count(self) -> Series | Index:
"""
Expand Down Expand Up @@ -3652,7 +3661,7 @@ def count(self, pat: str, flags: int = 0) -> Series | Index:
"unsupported value for `flags` parameter"
)
pat = self._remove_named_capture_groups(pat)
return self._return_or_inplace(self._column.count_re(pat, flags))
return self._return_integer(self._column.count_re(pat, flags))

def _findall(
self,
Expand Down Expand Up @@ -4273,7 +4282,7 @@ def index(
if (result == -1).any():
raise ValueError("substring not found")
else:
return result.astype(np.dtype(np.int64))
return result

def rindex(
self, sub: str, start: int = 0, end: int | None = None
Expand Down Expand Up @@ -4333,7 +4342,7 @@ def rindex(
if (result == -1).any():
raise ValueError("substring not found")
else:
return result.astype(np.dtype(np.int64))
return result

def match(
self,
Expand Down
24 changes: 0 additions & 24 deletions python/cudf/cudf/pandas/scripts/pandas-testing-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5503,7 +5503,6 @@ def pytest_unconfigure(config):
"tests/strings/test_extract.py::test_extract_series[string=string[pyarrow]-series_name]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_extract.py::test_extract_series[string=string[python]-None]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_extract.py::test_extract_series[string=str[python]-None]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_compiled_regex_flags[string=string[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_compiled_regex[string=object]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_compiled_regex[string=string[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_end_of_string[string=string[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
Expand All @@ -5512,35 +5511,12 @@ def pytest_unconfigure(config):
"tests/strings/test_find_replace.py::test_contains_lookarounds[string=str[pyarrow]-None-ab-expected_data4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_lookarounds[string=str[python]-na5-ab-expected_data4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_lookarounds[string=str[python]-None-ab-expected_data4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_moar[string=string[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_na_kwarg_for_nullable_string_dtype[string[pyarrow]-False-False-False]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_na_kwarg_for_nullable_string_dtype[string[pyarrow]-False-None-expected0]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_na_kwarg_for_nullable_string_dtype[string[pyarrow]-False-True-True]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_na_kwarg_for_nullable_string_dtype[string[pyarrow]-True-False-False]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_contains_na_kwarg_for_nullable_string_dtype[string[pyarrow]-True-None-expected0]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_replace_end_of_string[string=string[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_replace_end_of_string[string=str[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_startswith[False-None-object-pat1]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_find_replace.py::test_startswith[True-None-object-foo]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array_boolean_array[string[pyarrow]-isdigit-expected0]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array_boolean_array[string[pyarrow]-isnumeric-expected4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-contains]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-endswith2]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-endswith3]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-endswith4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-isdecimal]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-isdigit]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-istitle]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-len]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-startswith0]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-startswith1]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[pyarrow]-startswith3]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_string_array.py::test_string_array[string[python]-len]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_empty_str_methods[string=object]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_empty_str_methods[string=str[pyarrow]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_ismethods[string=string[pyarrow]-isalnum-expected1]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_ismethods[string=string[pyarrow]-isnumeric-expected4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_len[string=string[python]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_slice_replace[string=string[pyarrow]-None--2-z-expected5]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_slice_replace[string=string[python]--1-None-z-expected4]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/strings/test_strings.py::test_slice_replace[string=str[python]--10-3-z-expected7]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
Expand Down
54 changes: 54 additions & 0 deletions python/cudf/cudf/tests/series/accessors/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,60 @@ def test_getitem_out_of_bounds():
assert_eq(result, expected)


@pytest.mark.parametrize(
"dtype",
[
pd.StringDtype(storage="python"),
pd.StringDtype(storage="pyarrow"),
pd.StringDtype(storage="pyarrow", na_value=np.nan),
pd.ArrowDtype(pa.string()),
],
)
@pytest.mark.parametrize(
"method,args",
[
("contains", ("a",)),
("startswith", ("a",)),
("endswith", ("a",)),
("isdigit", ()),
("isnumeric", ()),
("isalnum", ()),
],
)
def test_string_predicate_extension_dtype(dtype, method, args):
ps = pd.Series(["a", None, "12"], dtype=dtype)
gs = cudf.from_pandas(ps)

expected = getattr(ps.str, method)(*args)
result = getattr(gs.str, method)(*args)

assert result.dtype == expected.dtype
assert_eq(result, expected)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved


@pytest.mark.parametrize("storage", ["python", "pyarrow"])
@pytest.mark.parametrize(
"method,args",
[
("len", ()),
("count", ("a",)),
("find", ("a",)),
("rfind", ("a",)),
("index", ("a",)),
("rindex", ("a",)),
],
)
def test_string_numeric_nullable_dtype(storage, method, args):
ps = pd.Series(["aba", None, "abc"], dtype=pd.StringDtype(storage=storage))
gs = cudf.from_pandas(ps)

expected = getattr(ps.str, method)(*args)
result = getattr(gs.str, method)(*args)

assert result.dtype == expected.dtype
assert_eq(result, expected)


@pytest.mark.parametrize("method", ["startswith", "endswith"])
@pytest.mark.parametrize("pat", [None, (1, 2), pd.Series([1])])
def test_startsendwith_invalid_pat(method, pat):
Expand Down
3 changes: 3 additions & 0 deletions python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@ def get_dtype_of_same_kind(source_dtype: DtypeObj, target_dtype: DtypeObj):
elif (
isinstance(source_dtype, pd.StringDtype)
and source_dtype.storage == "pyarrow"
and isinstance(target_dtype, cudf.ListDtype)
):
# Arrow-backed strings preserve Arrow storage for list results.
# Numeric and boolean results use pandas' nullable dtypes.
return dtype_to_pandas_arrowdtype(target_dtype)
return dtype_to_pandas_nullable_extension_type(target_dtype)
else:
Expand Down
Loading