Skip to content

REF: route groupby plotting through dedicated _python_apply_plot#65927

Open
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:ref-pinning
Open

REF: route groupby plotting through dedicated _python_apply_plot#65927
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:ref-pinning

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Behavior-neutral refactor split off from GH-64660 (the GH-41090 group-key name-pinning deprecation).

Groupby plotting — the .plot accessor, .plot.<method>(), and .hist() — previously went through _python_apply_general, relying on the group key being pinned to each group's name attribute as a side effect of the general apply path. This routes it through a dedicated _python_apply_plot helper that labels each Series group with its key explicitly.

This is a pure refactor (no behavior change). It expresses plotting's actual need — give each Series group its key so plotting methods can use it for legend labels — directly, instead of piggybacking on the name-pinning side effect. It also stands as groundwork for GH-41090: once plotting no longer depends on the pin, that deprecation can be enforced without touching plotting.

_python_apply_plot is equivalent to the general path for plotting:

  • Same (key, group) iteration (get_iterator is zip(result_index, _get_splitter(data)), exactly what apply_groupwise zips).
  • not_indexed_same is hardcoded True — plotting always returns matplotlib objects, which are never index-like, so the general path already computed True here.
  • Name labeling is narrowed to Series groups; DataFrame-group plotting takes legend labels from column names, never .name.

Test plan

  • pandas/tests/plotting/test_groupby.py passes, including a new test_groupby_plot_series_with_legend locking in key-as-legend-label through the new path
  • Full plotting + groupby suites pass (26534 passed, 0 failures)
  • pre-commit and mypy clean (no new errors)

🤖 Generated with Claude Code

Groupby plotting (the .plot accessor, .plot.<method>, and .hist) went
through _python_apply_general, relying on the group key being pinned to
each group's name attribute as a side effect of the general apply path.

Route it through a dedicated _python_apply_plot helper that labels each
Series group with its key explicitly. Behavior is unchanged; this
decouples plotting from the name-pinning side effect (groundwork for
GH#41090).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel requested a review from rhshadrach as a code owner June 21, 2026 19:40
@jbrockmendel jbrockmendel added Refactor Internal refactoring of code Groupby Visualization plotting labels Jun 21, 2026
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Groupby Refactor Internal refactoring of code Visualization plotting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant