Skip to content

BUG: pivot_table margins with EA dtype that cannot hold NA (GH#55484)#65301

Draft
jbrockmendel wants to merge 1 commit intopandas-dev:mainfrom
jbrockmendel:bug-55484
Draft

BUG: pivot_table margins with EA dtype that cannot hold NA (GH#55484)#65301
jbrockmendel wants to merge 1 commit intopandas-dev:mainfrom
jbrockmendel:bug-55484

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

  • In _add_margins, the no-columns branch initialized row_margin as float64 NaN, which raised TypeError when the grand-margin scalar was an ExtensionDtype scalar that couldn't be held by float64 (e.g. Interval with integer subtype).
  • Initialize row_margin as object dtype, recover the proper dtype with infer_objects after populating grand-margin values, and cast margin_dummy columns per target dtype (EA via astype, numeric via maybe_downcast_to_dtype, with an explicit astype fallback for object-source/numeric-target in mixed cases).
  • Replaces the misleading # Can hold NA already skip in the downcast loop with explicit branches.

closes #55484

Test plan

  • pytest pandas/tests/reshape/ — 2788 passed
  • New test_pivot_table_ea_dtype_cannot_hold_na_margins covers has-columns, no-columns, and mixed EA + numeric no-columns paths
  • pre-commit run --files on the touched paths

🤖 Generated with Claude Code

…GH#55484)

In the no-columns path, row_margin was initialized as float64 NaN,
which raised TypeError when the grand-margin scalar was an
ExtensionDtype scalar that cannot be held by float64 (e.g. an
Interval with integer subtype). Initialize as object dtype, recover
the proper dtype with infer_objects after the grand-margin values are
assigned, and cast margin_dummy columns explicitly per target dtype.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: pivot_table with EA dtype that cannot hold NA

1 participant