Skip to content

fix: stop calling the deprecated DataFrame.iterrows in Layout.get_details - #1159

Open
glaziermag wants to merge 1 commit into
has2k1:mainfrom
glaziermag:fix/layout-iterrows-deprecation
Open

glaziermag wants to merge 1 commit into
has2k1:mainfrom
glaziermag:fix/layout-iterrows-deprecation

Conversation

@glaziermag

Copy link
Copy Markdown

The weekly tests against pandas main fail again. pandas deprecated DataFrame.iterrows in pandas-dev/pandas#64719 (merged 2026-11-08, in the v3.2.0 whatsnew), and Layout.get_details loops over the panel layout with it (plotnine/facets/layout.py:311). That runs on every draw, so every plot emits a Pandas4Warning, and the latest-pandas jobs of the 2026-11-21 weekly run fail on the tests that check for warnings (https://github.com/has2k1/plotnine/actions/runs/44916327374):

FAILED tests/test_ggsave.py::TestArguments::test_save_method - AssertionError: Issued an unexpected warning
FAILED tests/test_guide_internals.py::test_no_after_scale_warning - pandas.errors.Pandas4Warning: DataFrame.iterrows is deprecated and will be removed in a future version. Use .itertuples instead.
FAILED tests/test_guide_internals.py::test_after_scale_positional_aesthetic_with_legend - pandas.errors.Pandas4Warning: ...
FAILED tests/test_position.py::test_stack_non_linear_scale - assert 3 == 1
FAILED tests/test_save_as_pdf_pages.py::TestArguments::test_save_as_pdf_method - AssertionError: Unexpected warnings

The loop now pairs the index with to_dict("records"), so panel_index and the row values it reads stay the same. pyright then reports the # type: ignore on panel_index as unnecessary, so that comment goes too.

To reproduce, with a pandas nightly (pip install --pre --extra-index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple pandas):

import warnings

import pandas as pd
from plotnine import aes, geom_point, ggplot
from plotnine.data import mtcars

warnings.simplefilter("error", pd.errors.Pandas4Warning)
(ggplot(mtcars, aes("wt", "mpg")) + geom_point()).draw()

Checks (macOS):

  • pandas 3.2.0.dev0+742.g8c7cd24e9e (Python 3.14): pytest tests gives 5 failed, 992 passed before and 997 passed after (warnings 1990 → 40).
  • pandas 3.1.1 (Python 3.14): 997 passed after.
  • pandas 2.2.0 (Python 3.11, --resolution=lowest-direct): the same test results before and after.
  • Layout.get_details() returns the same values with the same types before and after for facet_null, facet_wrap (fixed and free scales) and facet_grid (with and without margins), on all three pandas versions.
  • On my fork, in the weekly run https://github.com/glaziermag/plotnine/actions/runs/45675945814 both latest-pandas jobs pass; the three latest-matplotlib jobs fail on image comparisons with matplotlib main, which this does not touch. In the build run https://github.com/glaziermag/plotnine/actions/runs/45675945795 lint, typecheck and the 3.14 tests pass; the unittests jobs are red only because the Codecov upload needs a token the fork does not have.

#1145 also rewrites this loop as part of the narwhals port. This change does not depend on it.

Prepared with Claude Code (Claude Opus 5.5): it found the failing weekly run, wrote the change and ran the checks above.

🤖 Generated with Claude Code

pandas 3.2 deprecates DataFrame.iterrows (pandas-dev/pandas#64719).
Layout.get_details used it, so drawing any plot with pandas main emits
Pandas4Warning, and the latest-pandas weekly jobs fail on the tests that
check for warnings.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant