fix(pandas_postprocessing): avoid FutureWarning for mean/median in boxplot (#42004)

This commit is contained in:
Elizabeth Thompson
2026-07-17 15:02:40 -07:00
committed by GitHub
parent c84a154b52
commit 5750d82426
2 changed files with 31 additions and 3 deletions

View File

@@ -14,12 +14,15 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import warnings
import pytest
from superset.exceptions import InvalidPostProcessingError
from superset.utils.core import PostProcessingBoxplotWhiskerType
from superset.utils.pandas_postprocessing import boxplot
from tests.unit_tests.fixtures.dataframes import names_df
from tests.unit_tests.pandas_postprocessing.utils import series_to_list
def test_boxplot_tukey():
@@ -44,6 +47,27 @@ def test_boxplot_tukey():
assert len(df) == 4
def test_boxplot_mean_median_no_future_warning():
"""mean/median must be passed as strings (not np.mean/np.median) to
GroupBy.agg, else pandas raises a FutureWarning. Also verify the values
match a plain pandas groupby, since the string and callable forms could
silently diverge on a future pandas version."""
expected = names_df.groupby("region")["cars"].agg(["mean", "median"])
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
df = boxplot(
df=names_df,
groupby=["region"],
whisker_type=PostProcessingBoxplotWhiskerType.TUKEY,
metrics=["cars"],
)
df = df.set_index("region")
assert series_to_list(df["cars__mean"]) == series_to_list(expected["mean"])
assert series_to_list(df["cars__median"]) == series_to_list(expected["median"])
def test_boxplot_min_max():
df = boxplot(
df=names_df,