Skip to content

Commit 89fe3f4

Browse files
committed
is-finite for eager
1 parent 5c3db5b commit 89fe3f4

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

docs/api-reference/expr.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- clip
2222
- is_between
2323
- is_duplicated
24+
- is_finite
2425
- is_first_distinct
2526
- is_in
2627
- is_last_distinct

docs/api-reference/series.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- is_between
2727
- is_duplicated
2828
- is_empty
29+
- is_finite
2930
- is_first_distinct
3031
- is_in
3132
- is_last_distinct

narwhals/_arrow/expr.py

+3
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ def func(df: ArrowDataFrame) -> list[ArrowSeries]:
372372
def mode(self: Self) -> Self:
373373
return reuse_series_implementation(self, "mode")
374374

375+
def is_finite(self: Self) -> Self:
376+
return reuse_series_implementation(self, "is_finite")
377+
375378
@property
376379
def dt(self: Self) -> ArrowExprDateTimeNamespace:
377380
return ArrowExprDateTimeNamespace(self)

narwhals/_arrow/series.py

+5
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ def mode(self: Self) -> ArrowSeries:
721721
plx.col(col_token) == plx.col(col_token).max()
722722
)[self.name]
723723

724+
def is_finite(self: Self) -> Self:
725+
import pyarrow.compute as pc # ignore-banned-import
726+
727+
return self._from_native_series(pc.is_finite(self._native_series))
728+
724729
def __iter__(self: Self) -> Iterator[Any]:
725730
yield from self._native_series.__iter__()
726731

narwhals/_pandas_like/expr.py

+3
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ def gather_every(self: Self, n: int, offset: int = 0) -> Self:
387387
def mode(self: Self) -> Self:
388388
return reuse_series_implementation(self, "mode")
389389

390+
def is_finite(self: Self) -> Self:
391+
return reuse_series_implementation(self, "is_finite")
392+
390393
@property
391394
def str(self: Self) -> PandasLikeExprStringNamespace:
392395
return PandasLikeExprStringNamespace(self)

narwhals/_pandas_like/series.py

+5
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,11 @@ def mode(self: Self) -> Self:
686686
def __iter__(self: Self) -> Iterator[Any]:
687687
yield from self._native_series.__iter__()
688688

689+
def is_finite(self: Self) -> Self:
690+
import numpy as np # ignore-banned-import
691+
692+
return self._from_native_series(np.isfinite(self._native_series))
693+
689694
@property
690695
def str(self) -> PandasLikeSeriesStringNamespace:
691696
return PandasLikeSeriesStringNamespace(self)

narwhals/expr.py

+51
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,57 @@ def mode(self: Self) -> Self:
23132313
"""
23142314
return self.__class__(lambda plx: self._call(plx).mode())
23152315

2316+
def is_finite(self: Self) -> Self:
2317+
"""
2318+
Returns a boolean Series indicating which values are finite.
2319+
2320+
Returns:
2321+
Expression of `Boolean` data type.
2322+
2323+
Examples:
2324+
>>> import narwhals as nw
2325+
>>> import pandas as pd
2326+
>>> import polars as pl
2327+
>>> import pyarrow as pa
2328+
>>> data = {
2329+
... "a": [1.0, 2],
2330+
... "b": [3.0, float("inf")],
2331+
... }
2332+
2333+
We define a library agnostic function:
2334+
2335+
>>> @nw.narwhalify
2336+
... def func(df):
2337+
... return df.select(nw.all().is_finite())
2338+
2339+
We can then pass any supported library such as Pandas, Polars, or PyArrow to `func`:
2340+
2341+
>>> func(pd.DataFrame(data))
2342+
a b
2343+
0 True True
2344+
1 True False
2345+
2346+
>>> func(pl.DataFrame(data))
2347+
shape: (2, 2)
2348+
┌──────┬───────┐
2349+
│ a ┆ b │
2350+
│ --- ┆ --- │
2351+
│ bool ┆ bool │
2352+
╞══════╪═══════╡
2353+
│ true ┆ true │
2354+
│ true ┆ false │
2355+
└──────┴───────┘
2356+
2357+
>>> func(pa.table(data))
2358+
pyarrow.Table
2359+
a: bool
2360+
b: bool
2361+
----
2362+
a: [[true,true]]
2363+
b: [[true,false]]
2364+
"""
2365+
return self.__class__(lambda plx: self._call(plx).is_finite())
2366+
23162367
@property
23172368
def str(self: Self) -> ExprStringNamespace[Self]:
23182369
return ExprStringNamespace(self)

narwhals/series.py

+46
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,52 @@ def mode(self: Self) -> Self:
25252525
"""
25262526
return self._from_compliant_series(self._compliant_series.mode())
25272527

2528+
def is_finite(self: Self) -> Self:
2529+
"""
2530+
Returns a boolean Series indicating which values are finite.
2531+
2532+
Returns:
2533+
Expression of `Boolean` data type.
2534+
2535+
Examples:
2536+
>>> import narwhals as nw
2537+
>>> import pandas as pd
2538+
>>> import polars as pl
2539+
>>> import pyarrow as pa
2540+
>>> data = [1.0, float("inf")]
2541+
2542+
We define a library agnostic function:
2543+
2544+
>>> @nw.narwhalify
2545+
... def func(s):
2546+
... return s.is_finite()
2547+
2548+
We can then pass any supported library such as Pandas, Polars, or PyArrow to `func`:
2549+
2550+
>>> func(pd.Series(data))
2551+
0 True
2552+
1 False
2553+
dtype: bool
2554+
2555+
>>> func(pl.Series(data)) # doctest: +NORMALIZE_WHITESPACE
2556+
shape: (2,)
2557+
Series: '' [bool]
2558+
[
2559+
true
2560+
false
2561+
]
2562+
2563+
>>> func(pa.chunked_array([data])) # doctest: +ELLIPSIS
2564+
<pyarrow.lib.ChunkedArray object at ...>
2565+
[
2566+
[
2567+
true,
2568+
false
2569+
]
2570+
]
2571+
"""
2572+
return self._from_compliant_series(self._compliant_series.is_finite())
2573+
25282574
def __iter__(self: Self) -> Iterator[Any]:
25292575
yield from self._compliant_series.__iter__()
25302576

0 commit comments

Comments
 (0)