Skip to content

Support cudf-polars str.reverse #19117

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

Merged
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
4 changes: 4 additions & 0 deletions python/cudf_polars/cudf_polars/dsl/expressions/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def _validate_input(self) -> None:
StringFunction.Name.StripCharsStart,
StringFunction.Name.StripCharsEnd,
StringFunction.Name.Uppercase,
StringFunction.Name.Reverse,
StringFunction.Name.Tail,
StringFunction.Name.Titlecase,
):
Expand Down Expand Up @@ -450,6 +451,9 @@ def do_evaluate(
plc.strings.replace.replace_multiple(column.obj, target.obj, repl.obj),
dtype=self.dtype,
)
elif self.name is StringFunction.Name.Reverse:
(column,) = columns
return Column(plc.strings.reverse.reverse(column.obj), dtype=self.dtype)
elif self.name is StringFunction.Name.Titlecase:
(column,) = columns
return Column(plc.strings.capitalize.title(column.obj))
Expand Down
5 changes: 5 additions & 0 deletions python/cudf_polars/tests/expressions/test_stringfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ def test_string_join(ldf, ignore_nulls, delimiter):
assert_gpu_result_equal(q)


def test_string_reverse(ldf):
q = ldf.select(pl.col("a").str.reverse())
assert_gpu_result_equal(q)


def test_string_to_titlecase():
df = pl.LazyFrame(
{
Expand Down
10 changes: 10 additions & 0 deletions python/pylibcudf/pylibcudf/libcudf/strings/reverse.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025, NVIDIA CORPORATION.
from libcpp.memory cimport unique_ptr
from pylibcudf.exception_handler cimport libcudf_exception_handler
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.column.column_view cimport column_view

cdef extern from "cudf/strings/reverse.hpp" namespace "cudf::strings" nogil:
cdef unique_ptr[column] reverse(
column_view source_strings
) except +libcudf_exception_handler
3 changes: 2 additions & 1 deletion python/pylibcudf/pylibcudf/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# =============================================================================
# Copyright (c) 2024, NVIDIA CORPORATION.
# Copyright (c) 2024-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -29,6 +29,7 @@ set(cython_sources
repeat.pyx
replace.pyx
replace_re.pyx
reverse.pyx
side_type.pyx
slice.pyx
strip.pyx
Expand Down
4 changes: 3 additions & 1 deletion python/pylibcudf/pylibcudf/strings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
# Copyright (c) 2024-2025, NVIDIA CORPORATION.

from . import (
attributes,
Expand All @@ -18,6 +18,7 @@
repeat,
replace,
replace_re,
reverse,
side_type,
slice,
split,
Expand Down Expand Up @@ -46,6 +47,7 @@
"repeat",
"replace",
"replace_re",
"reverse",
"slice",
"split",
"strip",
Expand Down
32 changes: 32 additions & 0 deletions python/pylibcudf/pylibcudf/strings/reverse.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2024-2025, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move
from pylibcudf.column cimport Column
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.strings cimport reverse as cpp_reverse

__all__ = ["reverse"]

cpdef Column reverse(Column input):
"""Reverses the characters within each string.

Any null string entries return corresponding null output column entries.

For details, see :cpp:func:`reverse`.

Parameters
----------
input : Column
Strings column for this operation

Returns
-------
pylibcudf.Column
New strings column
"""
cdef unique_ptr[column] c_result
with nogil:
c_result = cpp_reverse.reverse(input.view())

return Column.from_libcudf(move(c_result))
29 changes: 29 additions & 0 deletions python/pylibcudf/pylibcudf/tests/test_string_reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2025, NVIDIA CORPORATION.

import pyarrow as pa
from utils import assert_column_eq

import pylibcudf as plc


def test_reverse():
arr = pa.array(
[
"bunny",
"rabbit",
"hare",
"dog",
"",
"a",
" leading",
"trailing ",
" mid dle ",
"123!@#",
None,
]
)
got = plc.strings.reverse.reverse(
plc.Column.from_arrow(arr),
)
expect = pa.compute.utf8_reverse(arr)
assert_column_eq(expect, got)