Skip to content

Commit

Permalink
docs: Add Pyarrow example to Expr.mean and nw.mean (#1207)
Browse files Browse the repository at this point in the history
* Add Pyarrow example to Expr.mean

* Make mean docstring consistent with stable v1 API
  • Loading branch information
CarloLepelaars authored Oct 17, 2024
1 parent e4fe3ee commit 1ce93de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 18 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 @@ -4054,17 +4063,19 @@ def mean(*columns: str) -> Expr:
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import pyarrow as pa
>>> import narwhals 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 @@ -4078,6 +4089,11 @@ def mean(*columns: str) -> Expr:
╞═════╡
│ 4.0 │
└─────┘
>>> func(df_pa)
pyarrow.Table
a: double
----
a: [[4]]
"""

return Expr(lambda plx: plx.mean(*columns))
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 @@ -1254,17 +1254,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 @@ -1278,6 +1280,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 1ce93de

Please sign in to comment.