From 90d14ad5d071d2e7971892a0c95bbd02860af75b Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Tue, 31 May 2022 20:36:34 -0400 Subject: [PATCH] Add percent option --- seaborn/_core/moves.py | 5 +++++ seaborn/tests/_core/test_moves.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/seaborn/_core/moves.py b/seaborn/_core/moves.py index 7eba537692..14469be83d 100644 --- a/seaborn/_core/moves.py +++ b/seaborn/_core/moves.py @@ -171,6 +171,7 @@ class Norm(Move): func: Union[Callable, str] = "max" where: Optional[str] = None by: Optional[list[str]] = None + percent: bool = False group_by_orient: ClassVar[bool] = False @@ -181,6 +182,10 @@ def _norm(self, df, var): else: denom_data = df.query(self.where)[var] df[var] = df[var] / denom_data.agg(self.func) + + if self.percent: + df[var] = df[var] * 100 + return df def __call__(self, data: DataFrame, groupby: GroupBy, orient: str) -> DataFrame: diff --git a/seaborn/tests/_core/test_moves.py b/seaborn/tests/_core/test_moves.py index 4d7f39cb0f..a9d221da19 100644 --- a/seaborn/tests/_core/test_moves.py +++ b/seaborn/tests/_core/test_moves.py @@ -350,3 +350,9 @@ def test_where(self, df): gb = GroupBy(["null"]) res = Norm(where="x == 2")(df, gb, "x") assert res.loc[res["x"] == 2, "y"].max() == pytest.approx(1) + + def test_percent(self, df): + + gb = GroupBy(["null"]) + res = Norm(percent=True)(df, gb, "x") + assert res["y"].max() == pytest.approx(100)