Skip to content

Commit

Permalink
Merge pull request #93 from brentomagic/sum
Browse files Browse the repository at this point in the history
Added sum() docstring
  • Loading branch information
MarcoGorelli authored May 6, 2024
2 parents 763d63c + be0abfa commit 61fda7e
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 @@ -349,6 +349,38 @@ def std(self, *, ddof: int = 1) -> Expr:
return self.__class__(lambda plx: self._call(plx).std(ddof=ddof))

def sum(self) -> Expr:
"""
Return the sum value.
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import narwhals as nw
>>> df_pd = pd.DataFrame({'a': [5, 10], 'b': [50, 100]})
>>> df_pl = pl.DataFrame({'a': [5, 10], 'b': [50, 100]})
Let's define a dataframe-agnostic function:
>>> def func(df_any):
... df = nw.from_native(df_any)
... df = df.select(nw.col('a', 'b').sum())
... return nw.to_native(df)
We can then pass either pandas or Polars to `func`:
>>> func(df_pd)
a b
0 15 150
>>> func(df_pl)
shape: (1, 2)
┌─────┬─────┐
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 15 ┆ 150 │
└─────┴─────┘
"""
return self.__class__(lambda plx: self._call(plx).sum())

def min(self) -> Expr:
Expand Down

0 comments on commit 61fda7e

Please sign in to comment.