Skip to content

ENH: add nanmean - #910

Open
ushnah wants to merge 2 commits into
data-apis:mainfrom
ushnah:nanmean
Open

ENH: add nanmean#910
ushnah wants to merge 2 commits into
data-apis:mainfrom
ushnah:nanmean

Conversation

@ushnah

@ushnah ushnah commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Towards #789

  • add support for nanmean

@lucascolley
lucascolley self-requested a review August 12, 2026 14:04
@lucascolley lucascolley added enhancement New feature or request new function labels Aug 12, 2026
@lucascolley lucascolley changed the title ENH: add nanmean ENH: add nanmean Aug 12, 2026
@lucascolley lucascolley added this to the 0.11.2 milestone Aug 12, 2026
@lucascolley

Copy link
Copy Markdown
Member

cc @OmarManzoor

@lucascolley
lucascolley removed their request for review August 12, 2026 18:08
@lucascolley lucascolley linked an issue Aug 12, 2026 that may be closed by this pull request
4 tasks

@qbarthelemy qbarthelemy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch must be rebased, because code has been moved to src\array_api_extra\_agnostic\_statistical.py

Comment thread src/array_api_extra/_lib/_funcs.py Outdated
Comment on lines +887 to +888
zero = xp.asarray(0, dtype=a.dtype, device=device_a)
sum_ = xp.sum(xp.where(mask, zero, a), axis=axis)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simply use nansum:

Suggested change
zero = xp.asarray(0, dtype=a.dtype, device=device_a)
sum_ = xp.sum(xp.where(mask, zero, a), axis=axis)
sum_ = nansum(a, axis=axis, xp=xp)

Comment thread src/array_api_extra/_lib/_funcs.py Outdated
sum_ = xp.sum(xp.where(mask, zero, a), axis=axis)
count = xp.count_nonzero(~mask, axis=axis)
safe_count = xp.astype(
xp.where(count == 0, xp.asarray(1, dtype=count.dtype, device=device_a), count),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xp.where(count == 0, xp.asarray(1, dtype=count.dtype, device=device_a), count),
xp.where(count == 0, xp.ones_like(a), count),

Comment thread src/array_api_extra/_lib/_funcs.py Outdated
if xp.any(count == 0):
result = xp.where(
count == 0,
xp.asarray(xp.nan, dtype=result.dtype, device=device_a),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xp.asarray(xp.nan, dtype=result.dtype, device=device_a),
xp.full_like(result, xp.nan),

With these three modifications, device_a is no longer necessary in nanmean.

@ushnah
ushnah force-pushed the nanmean branch 3 times, most recently from 4eaf451 to 1ba49d5 Compare August 23, 2026 22:46
Comment thread tests/main/test_statistical.py Outdated
applying changes for mean

Co-authored-by: Omar Salman <omar.salman@arbisoft.com>

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @ushnah

@OmarManzoor

Copy link
Copy Markdown
Contributor

CC: @lucascolley I think this looks fine to merge

@lucascolley
lucascolley self-requested a review August 25, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request new function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: add support for nanmin, nanmax, nanmean and nansum

5 participants