Skip to content

Commit

Permalink
add sign (#2977)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 25, 2022
1 parent 9e3e4e2 commit f5851b5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Computations
Expr.log
Expr.log10
Expr.exp
Expr.sign
Expr.unique_counts
Expr.value_counts

Expand Down
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Computations
Series.log
Series.log10
Series.exp
Series.sign

Manipulation/ selection
-----------------------
Expand Down
6 changes: 6 additions & 0 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,12 @@ def upper_bound(self) -> "Expr":
"""
return wrap_expr(self._pyexpr.upper_bound())

def sign(self) -> "Expr":
"""
Returns an element-wise indication of the sign of a number.
"""
return np.sign(self) # type: ignore

def sin(self) -> "Expr":
"""
Compute the element-wise value for Trigonometric sine on an array
Expand Down
6 changes: 6 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,12 @@ def mode(self) -> "Series":
"""
return wrap_s(self._s.mode())

def sign(self) -> "Series":
"""
Returns an element-wise indication of the sign of a number.
"""
return np.sign(self) # type: ignore

def sin(self) -> "Series":
"""
Compute the element-wise value for Trigonometric sine.
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1494,3 +1494,9 @@ def test_str_split() -> None:
assert out[0].to_list() == ["a,", " b"]
assert out[1].to_list() == ["a"]
assert out[2].to_list() == ["ab,", "c,", "de"]


def test_sign() -> None:
a = pl.Series("a", [10, -20, None])
expected = pl.Series("a", [1, -1, None])
verify_series_and_expr_api(a, expected, "sign")

0 comments on commit f5851b5

Please sign in to comment.