Skip to content

BUG: cast NaT to NaN when casting to float/complex - #32775

Closed
Hee-San wants to merge 1 commit into
numpy:mainfrom
Hee-San:nat-to-nan-float-cast
Closed

Hee-San wants to merge 1 commit into
numpy:mainfrom
Hee-San:nat-to-nan-float-cast

Conversation

@Hee-San

@Hee-San Hee-San commented Sep 24, 2026

Copy link
Copy Markdown

NaT is stored as the minimum int64 value, and casts to floating-point types used that value as is, giving -9.2e18. For float16 the value is out of range, so the cast gave -inf along with an overflow warning.

The reverse cast already maps NaN to NaT. To match it, map NaT to NaN. For complex types, the real part is NaN and the imaginary part is 0.

Casts to integer types and bool are unchanged. Integer types have no equivalent of NaN, and for int64 NaT stays the minimum value, so casting back to datetime gives NaT again. For bool, NaT is true, the same as NaN.

Closes #26177

PR summary

First time contributor introduction

AI Disclosure

NaT is stored as the minimum int64 value, and casts to floating-point
types used that value as is, giving -9.2e18. For float16 the value is
out of range, so the cast gave -inf along with an overflow warning.

The reverse cast already maps NaN to NaT. To match it, map NaT to NaN.
For complex types, the real part is NaN and the imaginary part is 0.

Casts to integer types and bool are unchanged. Integer types have no
equivalent of NaN, and for int64 NaT stays the minimum value, so
casting back to datetime gives NaT again. For bool, NaT is true, the
same as NaN.

Closes numpy#26177
@jorenham

Copy link
Copy Markdown
Member

You ignored the PR template, so I'm going to assume that this PR has been made with agentic AI. This violates our AI policy; we prefer to talk to humans.

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: NaT is incorrectly cast inside object arrays

2 participants