Skip to content

BUG: keep the dtype of an empty object on setitem-with-expansion (GH#19647) - #71961

Closed
jbrockmendel wants to merge 3 commits into
pandas-dev:mainfrom
jbrockmendel:bug-19647-partial
Closed

jbrockmendel wants to merge 3 commits into
pandas-dev:mainfrom
jbrockmendel:bug-19647-partial

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Partial fix for GH-19647: the dtype-retention piece only, not the broader "any"-dtype proposal.

Adding a new label via .loc or Series.__setitem__ to an empty Series or DataFrame with a non-object dtype now keeps that dtype when it can hold the value exactly. Previously the dtype was re-inferred from the value, so e.g. an empty float64 Series became int64 after ser.loc[0] = 1, and a later float insert then raised the GH-62369 incompatible-dtype warning.

  • Empty object dtype keeps inferring, since object is the default and says nothing about intent.
  • The dtype is kept only if the value round-trips unchanged (no True -> 1, no float rounding) and a later insert of the same value would also keep it. For example, a tz-mismatched Timestamp into an empty UTC column still follows main, because retaining UTC would send the next insert to object.

AI disclosure: drafted with Claude Code (claude opus 5.5 (high)), used for the implementation, tests, and iterative review against main.

…19647)

Adding a new label via .loc or Series.__setitem__ to an empty Series or
DataFrame with a non-object dtype now keeps that dtype when it can hold
the value exactly, instead of re-inferring from the value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jbrockmendel jbrockmendel added Bug Indexing Related to indexing on series/frames, not to indexes themselves Dtype Conversions Unexpected or buggy dtype conversions labels Nov 8, 2026
jbrockmendel and others added 2 commits November 8, 2026 14:46
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…GH#19647)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jbrockmendel

Copy link
Copy Markdown
Member Author

On reflection, I think we should just get a void dtype in place and get this right

@jbrockmendel
jbrockmendel deleted the bug-19647-partial branch November 9, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant