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: use type hints + from_native/to_native in series.py #1408

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
48 changes: 24 additions & 24 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,18 +876,18 @@ def clip(

We define a library agnostic function:

>>> @nw.narwhalify
... def func_lower(s):
... return s.clip(2)
>>> def clip_lower(s_native: IntoSeriesT) -> IntoSeriesT:
... s = nw.from_native(s_native, series_only=True)
... return s.clip(2).to_native()

We can then pass either pandas or Polars to `func_lower`:
We can then pass either pandas or Polars to `clip_lower`:

>>> func_lower(s_pd)
>>> clip_lower(s_pd)
0 2
1 2
2 3
dtype: int64
>>> func_lower(s_pl) # doctest: +NORMALIZE_WHITESPACE
>>> clip_lower(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
Expand All @@ -898,18 +898,18 @@ def clip(

We define another library agnostic function:

>>> @nw.narwhalify
... def func_upper(s):
... return s.clip(upper_bound=2)
>>> def clip_upper(s_native: IntoSeriesT) -> IntoSeriesT:
... s = nw.from_native(s_native, series_only=True)
... return s.clip(upper_bound=2).to_native()

We can then pass either pandas or Polars to `func_upper`:
We can then pass either pandas or Polars to `clip_upper`:

>>> func_upper(s_pd)
>>> clip_upper(s_pd)
0 1
1 2
2 2
dtype: int64
>>> func_upper(s_pl) # doctest: +NORMALIZE_WHITESPACE
>>> clip_upper(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
Expand Down Expand Up @@ -1582,23 +1582,23 @@ def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self:

We define library agnostic functions:

>>> def my_library_agnostic_function(s_native: IntoSeriesT) -> IntoSeriesT:
>>> def agnostic_sort(s_native: IntoSeriesT) -> IntoSeriesT:
... s = nw.from_native(s_native, series_only=True)
... return s.sort().to_native()

>>> @nw.narwhalify
... def func_descend(s):
>>> def agnostic_sort_descending(s_native: IntoSeriesT) -> IntoSeriesT:
... s = nw.from_native(s_native, series_only=True)
... return s.sort(descending=True).to_native()

We can then pass either pandas or Polars to `func`:
We can then pass either pandas or Polars to `agnostic_sort`:

>>> my_library_agnostic_function(s_pd)
>>> agnostic_sort(s_pd)
Comment on lines -1585 to +1595
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ€” ok i do like the way this looks...nice one @FBruzzesi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I like it as well! Drawback is... a bit more effort πŸ˜‚

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprint 😎

1 NaN
2 1.0
3 2.0
0 5.0
dtype: float64
>>> my_library_agnostic_function(s_pl) # doctest: +NORMALIZE_WHITESPACE
>>> agnostic_sort(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [i64]
[
Expand All @@ -1607,13 +1607,13 @@ def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self:
2
5
]
>>> func_descend(s_pd)
>>> agnostic_sort_descending(s_pd)
1 NaN
0 5.0
3 2.0
2 1.0
dtype: float64
>>> func_descend(s_pl) # doctest: +NORMALIZE_WHITESPACE
>>> agnostic_sort_descending(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [i64]
[
Expand Down Expand Up @@ -1719,17 +1719,17 @@ def fill_null(

Using a strategy:

>>> @nw.narwhalify
... def func_strategies(s):
>>> def my_library_agnostic_function(s_native: IntoSeriesT) -> IntoSeriesT:
... s = nw.from_native(s_native, series_only=True)
... return s.fill_null(strategy="forward", limit=1).to_native()

>>> func_strategies(s_pd)
>>> my_library_agnostic_function(s_pd)
0 1.0
1 2.0
2 2.0
dtype: float64

>>> func_strategies(s_pl) # doctest: +NORMALIZE_WHITESPACE
>>> my_library_agnostic_function(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
Expand Down
Loading