Skip to content

Commit

Permalink
Add percent option
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed Jun 1, 2022
1 parent 8e968b3 commit 90d14ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions seaborn/_core/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions seaborn/tests/_core/test_moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 90d14ad

Please sign in to comment.