|
| 1 | + |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +import pytest |
| 6 | +from numpy.testing import assert_array_equal |
| 7 | + |
| 8 | +from seaborn._core.groupby import GroupBy |
| 9 | +from seaborn._stats.order import Perc |
| 10 | +from seaborn.external.version import Version |
| 11 | + |
| 12 | + |
| 13 | +class Fixtures: |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def df(self, rng): |
| 17 | + return pd.DataFrame(dict(x="", y=rng.normal(size=30))) |
| 18 | + |
| 19 | + def get_groupby(self, df, orient): |
| 20 | + # TODO note, copied from aggregation |
| 21 | + other = {"x": "y", "y": "x"}[orient] |
| 22 | + cols = [c for c in df if c != other] |
| 23 | + return GroupBy(cols) |
| 24 | + |
| 25 | + |
| 26 | +class TestPerc(Fixtures): |
| 27 | + |
| 28 | + def test_int_k(self, df): |
| 29 | + |
| 30 | + ori = "x" |
| 31 | + gb = self.get_groupby(df, ori) |
| 32 | + res = Perc(3)(df, gb, ori, {}) |
| 33 | + percentiles = [0, 50, 100] |
| 34 | + assert_array_equal(res["percentile"], percentiles) |
| 35 | + assert_array_equal(res["y"], np.percentile(df["y"], percentiles)) |
| 36 | + |
| 37 | + def test_list_k(self, df): |
| 38 | + |
| 39 | + ori = "x" |
| 40 | + gb = self.get_groupby(df, ori) |
| 41 | + percentiles = [0, 20, 100] |
| 42 | + res = Perc(k=percentiles)(df, gb, ori, {}) |
| 43 | + assert_array_equal(res["percentile"], percentiles) |
| 44 | + assert_array_equal(res["y"], np.percentile(df["y"], percentiles)) |
| 45 | + |
| 46 | + def test_orientation(self, df): |
| 47 | + |
| 48 | + df = df.rename(columns={"x": "y", "y": "x"}) |
| 49 | + ori = "y" |
| 50 | + gb = self.get_groupby(df, ori) |
| 51 | + res = Perc(k=3)(df, gb, ori, {}) |
| 52 | + assert_array_equal(res["x"], np.percentile(df["x"], [0, 50, 100])) |
| 53 | + |
| 54 | + def test_method(self, df): |
| 55 | + |
| 56 | + ori = "x" |
| 57 | + gb = self.get_groupby(df, ori) |
| 58 | + method = "nearest" |
| 59 | + res = Perc(k=5, method=method)(df, gb, ori, {}) |
| 60 | + percentiles = [0, 25, 50, 75, 100] |
| 61 | + if Version(np.__version__) < Version("1.22.0"): |
| 62 | + expected = np.percentile(df["y"], percentiles, interpolation=method) |
| 63 | + else: |
| 64 | + expected = np.percentile(df["y"], percentiles, method=method) |
| 65 | + assert_array_equal(res["y"], expected) |
| 66 | + |
| 67 | + def test_grouped(self, df, rng): |
| 68 | + |
| 69 | + ori = "x" |
| 70 | + df = df.assign(x=rng.choice(["a", "b", "c"], len(df))) |
| 71 | + gb = self.get_groupby(df, ori) |
| 72 | + k = [10, 90] |
| 73 | + res = Perc(k)(df, gb, ori, {}) |
| 74 | + for x, res_x in res.groupby("x"): |
| 75 | + assert_array_equal(res_x["percentile"], k) |
| 76 | + expected = np.percentile(df.loc[df["x"] == x, "y"], k) |
| 77 | + assert_array_equal(res_x["y"], expected) |
| 78 | + |
| 79 | + def test_with_na(self, df): |
| 80 | + |
| 81 | + ori = "x" |
| 82 | + df.loc[:5, "y"] = np.nan |
| 83 | + gb = self.get_groupby(df, ori) |
| 84 | + k = [10, 90] |
| 85 | + res = Perc(k)(df, gb, ori, {}) |
| 86 | + expected = np.percentile(df["y"].dropna(), k) |
| 87 | + assert_array_equal(res["y"], expected) |
0 commit comments