Skip to content

Commit

Permalink
Merge pull request #82 from anopsy/newmean
Browse files Browse the repository at this point in the history
mean usage example added
  • Loading branch information
MarcoGorelli authored May 5, 2024
2 parents 79c07b3 + 4d67750 commit 620e599
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions narwhals/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,38 @@ def all(self) -> Expr:
return self.__class__(lambda plx: self._call(plx).all())

def mean(self) -> Expr:
"""
Get mean value.
Examples:
>>> import polars as pl
>>> import pandas as pd
>>> 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]})
Let's define a dataframe-agnostic function:
>>> def func(df_any):
... df = nw.from_native(df_any)
... df = df.select(nw.col('a', 'b').mean())
... return nw.to_native(df)
We can then pass either pandas or Polars to `func`:
>>> func(df_pd)
a b
0 0.0 4.0
>>> func(df_pl)
shape: (1, 2)
┌─────┬─────┐
│ a ┆ b │
│ --- ┆ --- │
│ f64 ┆ f64 │
╞═════╪═════╡
│ 0.0 ┆ 4.0 │
└─────┴─────┘
"""
return self.__class__(lambda plx: self._call(plx).mean())

def std(self) -> Expr:
Expand Down

0 comments on commit 620e599

Please sign in to comment.