Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
18 changes: 16 additions & 2 deletions python/ray/data/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ def abs(self) -> "UDFExpr":
"""
return _create_pyarrow_compute_udf(pc.abs_checked)(self)

# NULL Handling
def is_nan(self) -> "UDFExpr":
return _create_pyarrow_compute_udf(pc.is_nan, return_dtype=DataType.bool())(
self
)

def is_finite(self) -> "UDFExpr":
return _create_pyarrow_compute_udf(pc.is_finite, return_dtype=DataType.bool())(
self
)

def is_inf(self) -> "UDFExpr":
return _create_pyarrow_compute_udf(pc.is_inf, return_dtype=DataType.bool())(
self
)
Copy link

Choose a reason for hiding this comment

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

Missing docstrings on new public API methods

Low Severity

The new is_nan, is_finite, and is_inf methods are the only public methods on this class without docstrings. Every other method — including short ones like ceil, floor, and trunc — has at least a one-line docstring. This inconsistency makes these methods harder to discover and use, especially since they're part of a public API.

Fix in Cursor Fix in Web


@property
def arr(self) -> "_ArrayNamespace":
"""Access array operations for this expression."""
Expand Down Expand Up @@ -1236,13 +1252,11 @@ def to_arrow(val):
# Convert inputs to PyArrow and track pandas flags
args_results = [to_arrow(arg) for arg in args]
kwargs_results = {k: to_arrow(v) for k, v in kwargs.items()}

converted_args = [v[0] for v in args_results]
converted_kwargs = {k: v[0] for k, v in kwargs_results.items()}
input_was_pandas = any(v[1] for v in args_results) or any(
v[1] for v in kwargs_results.values()
)

# Call function with converted inputs
result = fn(*converted_args, **converted_kwargs)

Expand Down
52 changes: 52 additions & 0 deletions python/ray/data/tests/expressions/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,58 @@ def test_age_group_calculation_with_dataset(self, ray_start_regular_shared):
expected = pd.DataFrame({"age": [25, 17, 30], "age_group": [20, 10, 30]})
assert rows_same(result, expected)

@pytest.mark.parametrize(
"test_data, expr_factory, expected_results, test_id",
[
# Test is_nan
pytest.param(
[
{"x": float("nan")},
{"x": -3.0},
{"x": 0.0},
{"x": 3.14},
{"x": float("inf")},
{"x": float("-inf")},
{"x": None},
],
lambda: col("x").is_nan(),
[True, False, False, False, False, False, None],
"is_nan",
),
# Test is_finite
pytest.param(
[{"x": float("Inf")}, {"x": -3}, {"x": 0}],
lambda: col("x").is_finite(),
[False, True, True],
"is_finite",
),
# Test is_inf
pytest.param(
[{"x": float("Inf")}, {"x": -3}, {"x": 0}],
lambda: col("x").is_inf(),
[True, False, False],
"is_infinite",
),
],
)
def test_with_column_null_handling_operations(
ray_start_regular_shared,
test_data,
expr_factory,
expected_results,
test_id,
):
"""Test null handling helper expressions."""
ds = ray.data.from_items(test_data)
expr = expr_factory()
result_df = ds.with_column("result", expr).to_pandas()

# Create expected dataframe
expected_df = pd.DataFrame(test_data)
expected_df["result"] = expected_results

assert rows_same(result_df, expected_df)


if __name__ == "__main__":
import sys
Expand Down