From 85a0cfbf135ca177ad2bd74894a90012b714dae2 Mon Sep 17 00:00:00 2001 From: Nwabueze Ugoh <126014542+brentomagic@users.noreply.github.com> Date: Mon, 6 May 2024 17:38:44 +0100 Subject: [PATCH 1/2] Added sum() docstring --- narwhals/expression.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/narwhals/expression.py b/narwhals/expression.py index fc75c68aa..0ff993476 100644 --- a/narwhals/expression.py +++ b/narwhals/expression.py @@ -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(s). + + 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: From be0abfab767234ad44c6ceb11caf3020ea4c3a74 Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Mon, 6 May 2024 18:01:52 +0100 Subject: [PATCH 2/2] Update narwhals/expression.py --- narwhals/expression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/narwhals/expression.py b/narwhals/expression.py index 0ff993476..a441e7873 100644 --- a/narwhals/expression.py +++ b/narwhals/expression.py @@ -350,7 +350,7 @@ def std(self, *, ddof: int = 1) -> Expr: def sum(self) -> Expr: """ - Return the sum value(s). + Return the sum value. Examples: >>> import pandas as pd