diff --git a/plotnine/stats/stat_summary_bin.py b/plotnine/stats/stat_summary_bin.py index 5e92c269e8..47585eb944 100644 --- a/plotnine/stats/stat_summary_bin.py +++ b/plotnine/stats/stat_summary_bin.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING, cast +from warnings import warn import numpy as np import pandas as pd @@ -107,8 +108,9 @@ class stat_summary_bin(stat): def setup_params(self, data): keys = ("fun_data", "fun_y", "fun_ymin", "fun_ymax") if not any(self.params[k] for k in keys): - PlotnineWarning( - "No summary function, supplied, defaulting to mean_se()" + warn( + "No summary function supplied, defaulting to mean_se()", + PlotnineWarning, ) self.params["fun_data"] = "mean_se" diff --git a/tests/test_stat_summary_bin.py b/tests/test_stat_summary_bin.py index a80e63be9e..5f0dfd45c9 100644 --- a/tests/test_stat_summary_bin.py +++ b/tests/test_stat_summary_bin.py @@ -1,7 +1,9 @@ import numpy as np import pandas as pd +import pytest from plotnine import aes, ggplot, stat_summary_bin +from plotnine.exceptions import PlotnineWarning data = pd.DataFrame( { @@ -31,3 +33,11 @@ def test_continuous_x(): def test_setting_binwidth(): p = ggplot(data, aes("xc", "y")) + stat_summary_bin(binwidth=3, geom="bar") assert p == "setting_binwidth" + + +def test_no_summary_function_warns(): + # A missing summary function falls back to mean_se(), and the + # fallback must be announced. + p = ggplot(data, aes("xc", "y")) + stat_summary_bin(bins=5) + with pytest.warns(PlotnineWarning, match="defaulting to mean_se"): + p._build()