Skip to content

BUG: DataFrame.apply(raw=True) casting scalar results to the first result's dtype (GH#61632) - #71958

Merged
jbrockmendel merged 2 commits into
pandas-dev:mainfrom
jbrockmendel:bug-61632
Nov 9, 2026
Merged

jbrockmendel merged 2 commits into
pandas-dev:mainfrom
jbrockmendel:bug-61632

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Depends on GH-71926.

DataFrame.apply(raw=True) cast scalar results to the dtype of the first one (GH-61632), so 1 followed by 2.5 gave [1, 2, 2, 2], and 1 followed by nan gave [1, 0, 0, 0]. The dtype is now inferred from all results, as with raw=False. The docstring note added in GH-62384 is narrowed to list-like results.

List-like results keep the first-result dtype, so x if cond else x / 2 still truncates. Stacking with np.array(results) would fix that but corrupts datetime64[ns] mixed with float arrays into epoch ints, drops MaskedArray masks, and breaks funcs that reuse their output array.

Other behavior changes:

  • A scalar first result followed by containers gives an object Series, where main raised or unwrapped length-1 containers.
  • bytes results give an object Series instead of S3.
  • None followed by ints gives float64 with NaN instead of object.

Scalar funcs also get faster: 1.65-2.5x against main, up from 1.25-1.4x with GH-71926 alone.

AI disclosure: drafted with Claude Code (claude opus 5.5 (high)), used for the investigation, implementation, and iterative review of the branch.

🤖 Generated with Claude Code

jbrockmendel and others added 2 commits November 8, 2026 19:01
…sult's dtype (GH#61632)

Scalar results now infer their dtype from all results, as with
raw=False, instead of being cast to the dtype of the first, e.g. 1
followed by 2.5 gave 2. List-like results keep the first-result buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jbrockmendel jbrockmendel added this to the 3.2 milestone Nov 9, 2026
@jbrockmendel
jbrockmendel merged commit d78fcc7 into pandas-dev:main Nov 9, 2026
55 checks passed
@jbrockmendel
jbrockmendel deleted the bug-61632 branch November 9, 2026 04:38
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