Skip to content

BUG: fix nanmax/nanmin initial argument misbehaving in slow path#31102

Open
joshuaswanson wants to merge 1 commit intonumpy:mainfrom
joshuaswanson:fix/nanmax-initial
Open

BUG: fix nanmax/nanmin initial argument misbehaving in slow path#31102
joshuaswanson wants to merge 1 commit intonumpy:mainfrom
joshuaswanson:fix/nanmax-initial

Conversation

@joshuaswanson
Copy link
Copy Markdown
Contributor

Fixes #31087.

nanmin/nanmax misbehave with the initial argument in the slow path (non-ndarray input like lists). Two issues:

  1. nanmax([1.0], initial=nan) returns nan because the slow path uses np.amax which propagates NaN from initial, unlike np.fmax.reduce used in the fast path. Fixed by stripping NaN initial before passing to amax/amin, since nanmax/nanmin should ignore NaN in initial the same way they ignore NaN in array elements.

  2. nanmax([], initial=1.0) returns nan because the all-NaN check (np.all(mask)) returns True on an empty mask and unconditionally overwrites the result with NaN. Fixed by using initial as the result for all-NaN slices when it's a valid (non-NaN) value.

@ngoldbaum ngoldbaum modified the milestone: 2.4.5 release Apr 1, 2026
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: nanmax/nanmin, 'initial' argument misbehaves

3 participants