Skip to content

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

Open
eschutho wants to merge 2 commits into
apache:masterfrom
eschutho:fix/boxplot-mean-median-futurewarning
Open

fix(pandas_postprocessing): avoid FutureWarning for mean/median in boxplot#42004
eschutho wants to merge 2 commits into
apache:masterfrom
eschutho:fix/boxplot-mean-median-futurewarning

Conversation

@eschutho

Copy link
Copy Markdown
Member

What

Production logs showed a high-volume pandas FutureWarning:

The provided callable <function mean/median at 0x...> is currently using SeriesGroupBy.mean/median. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string "mean"/"median" instead.

This exact class of warning was previously addressed in #41025, which taught _get_aggregate_funcs (in pandas_postprocessing/utils.py) to pass the string operator name (e.g. "mean") instead of a raw numpy callable when possible, avoiding the ambiguity pandas warns about.

However, pandas_postprocessing/boxplot.py builds its own operators dict independently and was passing np.mean / np.median directly as the operator value. Since _get_aggregate_funcs special-cases callables first (if callable(operator): aggfunc = operator), this bypassed the #41025 fix entirely — so every boxplot chart post-processing call kept emitting the warning. Boxplot charts are common, which is why this was showing up frequently in prod logs.

Change

In boxplot.py, pass "mean" / "median" as string literals instead of np.mean / np.median, so they route through the same string-based path _get_aggregate_funcs already uses. np.ma.count is left untouched, since it intentionally differs from pandas' "count" (it includes NaNs).

No behavior change

Verified empirically that the string and callable forms produce identical output for mean/median on the currently pinned pandas range (>=2.1.4,<2.4). Added a regression test that also guards against a real (not just cosmetic) divergence: on pandas ≥3.0, np.median on a group containing NaNs returns NaN (no skipna), while the "median" string aggregator still skips NaNs — so this also prevents a silent numeric regression whenever the pandas pin is eventually bumped.

Test plan

  • Added test_boxplot_mean_median_no_future_warning in tests/unit_tests/pandas_postprocessing/test_boxplot.py: asserts no FutureWarning is raised and that mean/median values match a plain df.groupby(...).agg(["mean", "median"]) call.
  • Ran the full tests/unit_tests/pandas_postprocessing/ suite locally (108 tests, all passing).
  • Ran ruff check / ruff format and mypy on the changed file — no new issues introduced (two pre-existing, unrelated mypy errors on lines 103-104 remain untouched).

eschutho added 2 commits July 13, 2026 16:23
… avoid FutureWarning

boxplot() built its own operators map with raw np.mean/np.median
callables, bypassing the string-based fix from apache#41025 and still
triggering pandas' GroupBy.agg FutureWarning about ambiguous callables.
Passing the string names routes through the same code path that
already special-cases them, with no change in output.
…ian FutureWarning

Guards against the callable-vs-string aggfunc regression by asserting
no FutureWarning is raised and that mean/median values match a plain
pandas groupby.
@dosubot dosubot Bot added the viz:charts:boxplot Related to the Boxplot chart label Jul 13, 2026
@bito-code-review

bito-code-review Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #1de445

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 8ff873d..179c183
    • superset/utils/pandas_postprocessing/boxplot.py
    • tests/unit_tests/pandas_postprocessing/test_boxplot.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

assert len(df) == 4


def test_boxplot_mean_median_no_future_warning():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add an explicit return type annotation to this new test function signature (for example, -> None) to satisfy the type-hint requirement for newly introduced Python functions. [custom_rule]

Severity Level: Minor 🧹

Why it matters? ⭐

The new test function is missing an explicit return type annotation. Under the Python type-hint rule, newly introduced functions should be annotated, so -> None should be added here.

Rule source 📖

.cursor/rules/dev-standard.mdc (line 28)

Fix in Cursor Fix in VSCode Claude

(Use Cmd/Ctrl + Click for best experience)

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** tests/unit_tests/pandas_postprocessing/test_boxplot.py
**Line:** 50:50
**Comment:**
	*Custom Rule: Add an explicit return type annotation to this new test function signature (for example, `-> None`) to satisfy the type-hint requirement for newly introduced Python functions.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

@bito-code-review

Copy link
Copy Markdown
Contributor

The suggestion to add an explicit return type annotation to the new test function is correct and follows standard Python type-hinting practices. You can resolve this by updating the function signature to include -> None.

Here is the corrected function signature:

def test_boxplot_mean_median_no_future_warning() -> None:
    """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."""

There are no other review comments available in the provided context to check. Would you like me to proceed with any other tasks?

tests/unit_tests/pandas_postprocessing/test_boxplot.py

def test_boxplot_mean_median_no_future_warning() -> None:
    """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."""

@eschutho eschutho requested a review from rebenitez1802 July 13, 2026 16:37
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 65.01%. Comparing base (8f75f1a) to head (179c183).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
superset/utils/pandas_postprocessing/boxplot.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #42004   +/-   ##
=======================================
  Coverage   65.01%   65.01%           
=======================================
  Files        2744     2744           
  Lines      153571   153571           
  Branches    35212    35212           
=======================================
  Hits        99842    99842           
  Misses      51822    51822           
  Partials     1907     1907           
Flag Coverage Δ
hive 39.06% <0.00%> (ø)
mysql 57.92% <0.00%> (ø)
postgres 57.98% <0.00%> (ø)
presto 41.05% <0.00%> (ø)
python 59.36% <0.00%> (ø)
sqlite 57.59% <0.00%> (ø)
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M viz:charts:boxplot Related to the Boxplot chart

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant