Skip to content

Commit

Permalink
fix(python): accept tuple[bool, bool] instead of Sequence[bool] f…
Browse files Browse the repository at this point in the history
…or `Expr.is_between` (#5094)
  • Loading branch information
matteosantama committed Oct 17, 2022
1 parent f32b44a commit cafb7cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
36 changes: 19 additions & 17 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import math
import random
import warnings
from datetime import date, datetime, time, timedelta
from typing import TYPE_CHECKING, Any, Callable, NoReturn, Sequence
from warnings import warn
Expand Down Expand Up @@ -3428,7 +3429,7 @@ def is_between(
self,
start: Expr | datetime | int,
end: Expr | datetime | int,
include_bounds: bool | Sequence[bool] = False,
include_bounds: bool | tuple[bool, bool] = False,
) -> Expr:
"""
Check if this expression is between start and end.
Expand All @@ -3442,22 +3443,18 @@ def is_between(
include_bounds
False: Exclude both start and end (default).
True: Include both start and end.
[False, False]: Exclude start and exclude end.
[True, True]: Include start and include end.
[False, True]: Exclude start and include end.
[True, False]: Include start and exclude end.
(False, False): Exclude start and exclude end.
(True, True): Include start and include end.
(False, True): Exclude start and include end.
(True, False): Include start and exclude end.
Returns
-------
Expr that evaluates to a Boolean Series.
Examples
--------
>>> df = pl.DataFrame(
... {
... "num": [1, 2, 3, 4, 5],
... }
... )
>>> df = pl.DataFrame({"num": [1, 2, 3, 4, 5]})
>>> df.with_column(pl.col("num").is_between(2, 4))
shape: (5, 2)
┌─────┬────────────┐
Expand Down Expand Up @@ -3488,18 +3485,23 @@ def is_between(
expr = self.cast(Datetime)
else:
expr = self
if include_bounds is False or include_bounds == [False, False]:
if isinstance(include_bounds, list):
warnings.warn(
"include_bounds: list[bool] will not be supported in a future "
"version; pass include_bounds: tuple[bool, bool] instead",
category=DeprecationWarning,
)
include_bounds = tuple(include_bounds)
if include_bounds is False or include_bounds == (False, False):
return ((expr > start) & (expr < end)).alias("is_between")
elif include_bounds is True or include_bounds == [True, True]:
elif include_bounds is True or include_bounds == (True, True):
return ((expr >= start) & (expr <= end)).alias("is_between")
elif include_bounds == [False, True]:
elif include_bounds == (False, True):
return ((expr > start) & (expr <= end)).alias("is_between")
elif include_bounds == [True, False]:
elif include_bounds == (True, False):
return ((expr >= start) & (expr < end)).alias("is_between")
else:
raise ValueError(
"include_bounds should be a boolean or [boolean, boolean]."
)
raise ValueError("include_bounds should be a bool or tuple[bool, bool].")

def hash(
self,
Expand Down
8 changes: 4 additions & 4 deletions py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ def test_is_between(fruits_cars: pl.DataFrame) -> None:
pl.Series("is_between", [False, False, True, False, False])
)

result = fruits_cars.select(pl.col("A").is_between(2, 4, [False, False]))[
result = fruits_cars.select(pl.col("A").is_between(2, 4, (False, False)))[
"is_between"
]
assert result.series_equal(
Expand All @@ -1195,21 +1195,21 @@ def test_is_between(fruits_cars: pl.DataFrame) -> None:
pl.Series("is_between", [False, True, True, True, False])
)

result = fruits_cars.select(pl.col("A").is_between(2, 4, [True, True]))[
result = fruits_cars.select(pl.col("A").is_between(2, 4, (True, True)))[
"is_between"
]
assert result.series_equal(
pl.Series("is_between", [False, True, True, True, False])
)

result = fruits_cars.select(pl.col("A").is_between(2, 4, [False, True]))[
result = fruits_cars.select(pl.col("A").is_between(2, 4, (False, True)))[
"is_between"
]
assert result.series_equal(
pl.Series("is_between", [False, False, True, True, False])
)

result = fruits_cars.select(pl.col("A").is_between(2, 4, [True, False]))[
result = fruits_cars.select(pl.col("A").is_between(2, 4, (True, False)))[
"is_between"
]
assert result.series_equal(
Expand Down

0 comments on commit cafb7cf

Please sign in to comment.