Skip to content

Commit

Permalink
Make mean docstring consistent with stable v1 API
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLepelaars committed Oct 17, 2024
1 parent 84dcc03 commit 9f74619
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 11 additions & 2 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,19 @@ def mean(self) -> Self:
Examples:
>>> import polars as pl
>>> import pandas as pd
>>> import pyarrow as pa
>>> import narwhals as nw
>>> df_pd = pd.DataFrame({"a": [-1, 0, 1], "b": [2, 4, 6]})
>>> df_pl = pl.DataFrame({"a": [-1, 0, 1], "b": [2, 4, 6]})
>>> df_pa = pa.table({"a": [-1, 0, 1], "b": [2, 4, 6]})
Let's define a dataframe-agnostic function:
>>> @nw.narwhalify
... def func(df):
... return df.select(nw.col("a", "b").mean())
We can then pass either pandas or Polars to `func`:
We can pass any supported library such as Pandas, Polars, or PyArrow to `func`:
>>> func(df_pd)
a b
Expand All @@ -391,6 +393,13 @@ def mean(self) -> Self:
╞═════╪═════╡
│ 0.0 ┆ 4.0 │
└─────┴─────┘
>>> func(df_pa)
pyarrow.Table
a: double
b: double
----
a: [[0]]
b: [[4]]
"""
return self.__class__(lambda plx: self._call(plx).mean())

Expand Down Expand Up @@ -4068,7 +4077,7 @@ def mean(*columns: str) -> Expr:
... def func(df):
... return df.select(nw.mean("a"))
We can pass any supported library such as pandas, Polars, or PyArrow to `func`:
We can pass any supported library such as Pandas, Polars, or PyArrow to `func`:
>>> func(df_pd)
a
Expand Down
9 changes: 8 additions & 1 deletion narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,17 +1250,19 @@ def mean(*columns: str) -> Expr:
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import pyarrow as pa
>>> import narwhals.stable.v1 as nw
>>> df_pl = pl.DataFrame({"a": [1, 8, 3]})
>>> df_pd = pd.DataFrame({"a": [1, 8, 3]})
>>> df_pa = pa.table({"a": [1, 8, 3]})
We define a dataframe agnostic function:
>>> @nw.narwhalify
... def func(df):
... return df.select(nw.mean("a"))
We can then pass either pandas or Polars to `func`:
We can pass any supported library such as Pandas, Polars, or PyArrow to `func`:
>>> func(df_pd)
a
Expand All @@ -1274,6 +1276,11 @@ def mean(*columns: str) -> Expr:
╞═════╡
│ 4.0 │
└─────┘
>>> func(df_pa)
pyarrow.Table
a: double
----
a: [[4]]
"""
return _stableify(nw.mean(*columns))

Expand Down

0 comments on commit 9f74619

Please sign in to comment.