BUG: pivot_table margins with EA dtype that cannot hold NA (GH#55484)#65301
Draft
jbrockmendel wants to merge 1 commit intopandas-dev:mainfrom
Draft
BUG: pivot_table margins with EA dtype that cannot hold NA (GH#55484)#65301jbrockmendel wants to merge 1 commit intopandas-dev:mainfrom
jbrockmendel wants to merge 1 commit intopandas-dev:mainfrom
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_add_margins, the no-columns branch initializedrow_marginas float64 NaN, which raisedTypeErrorwhen the grand-margin scalar was anExtensionDtypescalar that couldn't be held by float64 (e.g.Intervalwith integer subtype).row_marginas object dtype, recover the proper dtype withinfer_objectsafter populating grand-margin values, and castmargin_dummycolumns per target dtype (EA viaastype, numeric viamaybe_downcast_to_dtype, with an explicitastypefallback for object-source/numeric-target in mixed cases).# Can hold NA alreadyskip in the downcast loop with explicit branches.closes #55484
Test plan
pytest pandas/tests/reshape/— 2788 passedtest_pivot_table_ea_dtype_cannot_hold_na_marginscovers has-columns, no-columns, and mixed EA + numeric no-columns pathspre-commit run --fileson the touched paths🤖 Generated with Claude Code