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

cudf-polars string/numeric casting #17076

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3d0dc8a
fix
brandon-b-miller Oct 14, 2024
59ceb03
passing
brandon-b-miller Oct 14, 2024
be0fae9
more tests, needs refactor
brandon-b-miller Oct 14, 2024
209b906
refactor
brandon-b-miller Oct 14, 2024
0258478
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
Matt711 Oct 15, 2024
c9199ec
address reviews
brandon-b-miller Oct 15, 2024
7feb1f3
update tests
brandon-b-miller Oct 16, 2024
6d699ac
handle runtime conversion failure
brandon-b-miller Oct 16, 2024
f29a918
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Oct 22, 2024
735c9e3
moving things
brandon-b-miller Oct 24, 2024
9f2cc18
implement and use is_numeric_not_bool
brandon-b-miller Oct 25, 2024
9ecac41
update tests
brandon-b-miller Oct 25, 2024
740af73
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Oct 28, 2024
cd80083
test, implement, and use is_order_preserving_cast
brandon-b-miller Oct 28, 2024
f98f635
small fix
brandon-b-miller Oct 28, 2024
7f75375
pass test_string_from_float
brandon-b-miller Oct 28, 2024
9c9d395
add failing test to plugin xfail list
brandon-b-miller Oct 28, 2024
00bd36c
Update python/cudf_polars/cudf_polars/testing/plugin.py
brandon-b-miller Oct 29, 2024
c06f984
Update python/cudf_polars/cudf_polars/utils/dtypes.py
brandon-b-miller Oct 29, 2024
a68011a
minor fixups
brandon-b-miller Oct 29, 2024
09d8e48
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Oct 29, 2024
334eef4
merge/resolve
brandon-b-miller Nov 1, 2024
3c45ffb
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Nov 4, 2024
cf62714
allow float to int
brandon-b-miller Nov 5, 2024
0344f53
small fixes
brandon-b-miller Nov 5, 2024
9cfc487
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Nov 5, 2024
fdd5abc
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
brandon-b-miller Nov 6, 2024
69432a1
Merge branch 'branch-24.12' into cudf-polars-string-numeric-casting
vyasr Nov 7, 2024
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
43 changes: 41 additions & 2 deletions python/cudf_polars/cudf_polars/dsl/expressions/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pyarrow as pa
import pylibcudf as plc
from pylibcudf.traits import is_floating_point, is_integral_not_bool

from cudf_polars.containers import Column
from cudf_polars.dsl.expressions.base import AggInfo, ExecutionContext, Expr
Expand All @@ -23,6 +24,10 @@
__all__ = ["Cast", "UnaryFunction", "Len"]


def _is_int_or_float(dtype: plc.DataType) -> bool:
return is_integral_not_bool(dtype) or is_floating_point(dtype)


class Cast(Expr):
"""Class representing a cast of an expression."""

Expand All @@ -33,7 +38,19 @@ class Cast(Expr):
def __init__(self, dtype: plc.DataType, value: Expr) -> None:
super().__init__(dtype)
self.children = (value,)
if not dtypes.can_cast(value.dtype, self.dtype):
if (
self.dtype.id() == plc.TypeId.STRING
or value.dtype.id() == plc.TypeId.STRING
):
if not (
(self.dtype.id() == plc.TypeId.STRING and _is_int_or_float(value.dtype))
or (
_is_int_or_float(self.dtype)
and value.dtype.id() == plc.TypeId.STRING
)
):
raise NotImplementedError("Only string to float cast is supported")
elif not dtypes.can_cast(value.dtype, self.dtype):
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError(
f"Can't cast {self.dtype.id().name} to {value.dtype.id().name}"
)
Expand All @@ -48,7 +65,29 @@ def do_evaluate(
"""Evaluate this expression given a dataframe for context."""
(child,) = self.children
column = child.evaluate(df, context=context, mapping=mapping)
return Column(plc.unary.cast(column.obj, self.dtype)).sorted_like(column)
if (
self.dtype.id() == plc.TypeId.STRING
or column.obj.type().id() == plc.TypeId.STRING
):
if self.dtype.id() == plc.TypeId.STRING:
if is_floating_point(column.obj.type()):
result = plc.strings.convert.convert_floats.from_floats(column.obj)
else:
result = plc.strings.convert.convert_integers.from_integers(
column.obj
)
else:
if is_floating_point(self.dtype):
result = plc.strings.convert.convert_floats.to_floats(
column.obj, self.dtype
)
else:
result = plc.strings.convert.convert_integers.to_integers(
column.obj, self.dtype
)
else:
result = plc.unary.cast(column.obj, self.dtype)
return Column(result).sorted_like(column)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved

def collect_agg(self, *, depth: int) -> AggInfo:
"""Collect information about aggregations in groupbys."""
Expand Down
38 changes: 38 additions & 0 deletions python/cudf_polars/tests/expressions/test_stringfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ def ldf(with_nulls):
)


@pytest.fixture(params=[pl.Float32, pl.Float64, pl.Int8, pl.Int16, pl.Int32, pl.Int64])
def numeric_type(request):
return request.param


@pytest.fixture
def str_to_numeric_data(with_nulls):
a = ["1", "2", "3", "4", "5", "6"]
if with_nulls:
a[4] = None
return pl.LazyFrame({"a": a})


@pytest.fixture
def str_from_numeric_data(with_nulls, numeric_type):
a = [
1,
2,
3,
4,
5,
6,
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this will go on one line if we remove the final trailing comma.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For floating point types, can we please test:

  • negative numbers
  • +/- inf
  • nan
  • scientific notation

In addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, libcudf gives us Inf and polars gives us inf. :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does polars ingest Inf fine?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, just compare case insensitively

if with_nulls:
a[4] = None
return pl.LazyFrame({"a": pl.Series(a, dtype=numeric_type)})


slice_cases = [
(1, 3),
(0, 3),
Expand Down Expand Up @@ -337,3 +365,13 @@ def test_unsupported_regex_raises(pattern):

q = df.select(pl.col("a").str.contains(pattern, strict=True))
assert_ir_translation_raises(q, NotImplementedError)


def test_string_to_numeric(str_to_numeric_data, numeric_type):
query = str_to_numeric_data.select(pl.col("a").cast(numeric_type))
assert_gpu_result_equal(query)


def test_string_from_numeric(str_from_numeric_data):
query = str_from_numeric_data.select(pl.col("a").cast(pl.String))
assert_gpu_result_equal(query)
Loading