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

docs: fix is_between type hint in signature #1766

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion narwhals/_arrow/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ def is_null(self: Self) -> Self:
def is_nan(self: Self) -> Self:
return reuse_series_implementation(self, "is_nan")

def is_between(self: Self, lower_bound: Any, upper_bound: Any, closed: str) -> Self:
def is_between(
self: Self,
lower_bound: Any,
upper_bound: Any,
closed: Literal["left", "right", "none", "both"],
) -> Self:
return reuse_series_implementation(
self,
"is_between",
Expand Down
5 changes: 4 additions & 1 deletion narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,10 @@ def all(self: Self, *, _return_py_scalar: bool = True) -> bool:
)

def is_between(
self, lower_bound: Any, upper_bound: Any, closed: str = "both"
self,
lower_bound: Any,
upper_bound: Any,
closed: Literal["left", "right", "none", "both"],
) -> Self:
import pyarrow.compute as pc

Expand Down
7 changes: 3 additions & 4 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,17 @@ def is_between(
self,
lower_bound: Self | Any,
upper_bound: Self | Any,
closed: str = "both",
closed: Literal["left", "right", "none", "both"],
) -> Self:
if closed == "none":
closed = "neither"
closed_ = "neither" if closed == "none" else closed
return self._from_call(
lambda _input, lower_bound, upper_bound, closed: _input.between(
lower_bound, upper_bound, closed
),
"is_between",
lower_bound=lower_bound,
upper_bound=upper_bound,
closed=closed,
closed=closed_,
returns_scalar=self._returns_scalar,
)

Expand Down
5 changes: 4 additions & 1 deletion narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ def clip(self, lower_bound: Any, upper_bound: Any) -> Self:
)

def is_between(
self, lower_bound: Any, upper_bound: Any, closed: str = "both"
self,
lower_bound: Any,
upper_bound: Any,
closed: Literal["left", "right", "none", "both"],
) -> Self:
return reuse_series_implementation(
self,
Expand Down
5 changes: 4 additions & 1 deletion narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ def to_list(self) -> Any:
return self._native_series.to_list()

def is_between(
self, lower_bound: Any, upper_bound: Any, closed: str = "both"
self,
lower_bound: Any,
upper_bound: Any,
closed: Literal["left", "right", "none", "both"],
) -> PandasLikeSeries:
ser = self._native_series
_, lower_bound = broadcast_align_and_extract_native(self, lower_bound)
Expand Down
3 changes: 2 additions & 1 deletion narwhals/_spark_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Literal
from typing import Sequence

from narwhals._expression_parsing import infer_new_root_output_names
Expand Down Expand Up @@ -276,7 +277,7 @@ def is_between(
self,
lower_bound: Any,
upper_bound: Any,
closed: str,
closed: Literal["left", "right", "none", "both"],
) -> Self:
def _is_between(_input: Column, lower_bound: Any, upper_bound: Any) -> Column:
if closed == "both":
Expand Down
4 changes: 2 additions & 2 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,10 +1792,10 @@ def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self:

# --- transform ---
def is_between(
self,
self: Self,
lower_bound: Any | IntoExpr,
upper_bound: Any | IntoExpr,
closed: str = "both",
closed: Literal["left", "right", "none", "both"] = "both",
) -> Self:
"""Check if this expression is between the given lower and upper bounds.

Expand Down
5 changes: 4 additions & 1 deletion narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,10 @@ def fill_null(
)

def is_between(
self, lower_bound: Any | Self, upper_bound: Any | Self, closed: str = "both"
self: Self,
lower_bound: Any | Self,
upper_bound: Any | Self,
closed: Literal["left", "right", "none", "both"] = "both",
) -> Self:
"""Get a boolean mask of the values that are between the given lower/upper bounds.

Expand Down
12 changes: 10 additions & 2 deletions tests/expr_and_series/is_between_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import Literal

import pytest

import narwhals.stable.v1 as nw
Expand All @@ -17,7 +19,11 @@
("none", [False, True, True, False]),
],
)
def test_is_between(constructor: Constructor, closed: str, expected: list[bool]) -> None:
def test_is_between(
constructor: Constructor,
closed: Literal["left", "right", "none", "both"],
expected: list[bool],
) -> None:
data = {"a": [1, 4, 2, 5]}
df = nw.from_native(constructor(data))
result = df.select(nw.col("a").is_between(1, 5, closed=closed))
Expand All @@ -43,7 +49,9 @@ def test_is_between_expressified(constructor: Constructor) -> None:
],
)
def test_is_between_series(
constructor_eager: ConstructorEager, closed: str, expected: list[bool]
constructor_eager: ConstructorEager,
closed: Literal["left", "right", "none", "both"],
expected: list[bool],
) -> None:
data = {"a": [1, 4, 2, 5]}
df = nw.from_native(constructor_eager(data), eager_only=True)
Expand Down
5 changes: 4 additions & 1 deletion tests/spark_like_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from contextlib import nullcontext as does_not_raise
from typing import TYPE_CHECKING
from typing import Any
from typing import Literal

import pandas as pd
import pytest
Expand Down Expand Up @@ -991,7 +992,9 @@ def test_clip(pyspark_constructor: Constructor) -> None:
],
)
def test_is_between(
pyspark_constructor: Constructor, closed: str, expected: list[bool]
pyspark_constructor: Constructor,
closed: Literal["left", "right", "none", "both"],
expected: list[bool],
) -> None:
data = {"a": [1, 4, 2, 5]}
df = nw.from_native(pyspark_constructor(data))
Expand Down
Loading