Skip to content

Commit

Permalink
fix(python): Support numpy ufunc when expression not first arg (#6675)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj authored Feb 5, 2023
1 parent 96490f2 commit 25c5cdc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ def __array_ufunc__(
self, ufunc: Callable[..., Any], method: str, *inputs: Any, **kwargs: Any
) -> Expr:
"""Numpy universal functions."""
args = [inp for inp in inputs if not isinstance(inp, Expr)]

def function(s: pli.Series) -> pli.Series: # pragma: no cover
return ufunc(s, *args, **kwargs)
args = [inp if not isinstance(inp, Expr) else s for inp in inputs]
return ufunc(*args, **kwargs)

return self.map(function)

Expand Down
20 changes: 20 additions & 0 deletions py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,26 @@ def test_ufunc() -> None:
assert out.dtypes == expected.dtypes


def test_ufunc_expr_not_first() -> None:
"""Check numpy ufunc expressions also work if expression not the first argument."""
df = pl.DataFrame([pl.Series("a", [1, 2, 3], dtype=pl.Float64)])
out = df.select(
[
np.power(2.0, cast(Any, pl.col("a"))).alias("power"),
(2.0 / cast(Any, pl.col("a"))).alias("divide_scalar"),
(np.array([2, 2, 2]) / cast(Any, pl.col("a"))).alias("divide_array"),
]
)
expected = pl.DataFrame(
[
pl.Series("power", [2**1, 2**2, 2**3], dtype=pl.Float64),
pl.Series("divide_scalar", [2 / 1, 2 / 2, 2 / 3], dtype=pl.Float64),
pl.Series("divide_array", [2 / 1, 2 / 2, 2 / 3], dtype=pl.Float64),
]
)
assert_frame_equal(out, expected)


def test_clip() -> None:
df = pl.DataFrame({"a": [1, 2, 3, 4, 5]})
assert df.select(pl.col("a").clip(2, 4))["a"].to_list() == [2, 2, 3, 4, 4]
Expand Down

0 comments on commit 25c5cdc

Please sign in to comment.