Skip to content

ENH: add nanmean - #910

Open
ushnah wants to merge 1 commit into
data-apis:mainfrom
ushnah:nanmean
Open

ENH: add nanmean#910
ushnah wants to merge 1 commit 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 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)

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),

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.

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

4 participants