Skip to content

BUG: Pass DType class np.average to support weights argument for user-dtypes#31575

Open
SwayamInSync wants to merge 1 commit into
numpy:mainfrom
SwayamInSync:fix/ufunc-dtype-user-instance
Open

BUG: Pass DType class np.average to support weights argument for user-dtypes#31575
SwayamInSync wants to merge 1 commit into
numpy:mainfrom
SwayamInSync:fix/ufunc-dtype-user-instance

Conversation

@SwayamInSync

@SwayamInSync SwayamInSync commented Jun 6, 2026

Copy link
Copy Markdown
Member

PR summary

Same fix as #31447

Discovered at numpy/numpy-quaddtype#106

AI Disclosure

None

@SwayamInSync SwayamInSync changed the title BUG: Pass DType class, not instance to np.average to support weights argument for user-dtypes BUG: Pass DType class np.average to support weights argument for user-dtypes Jun 6, 2026

a = np.array([1, 2, 3, 4], dtype=qd)
w = np.array([4, 3, 2, 1], dtype=qd)
assert float(np.average(a)) == 2.5

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.

I think one should test that the returned array has the right data type, with, e.g.,

assert_array_equal(np.average(a), np.array(2.5, dtype=qd), strict=True)

Note that it might also be good to make sure the precision is in fact kept, e.g., by writing

a = np.array([1, 2, 3, 4], dtype=qd) + np.array(1e-20, dtype=dq)
w = np.array([4, 3, 2, 1], dtype=qd) - np.array(1e-21, dtype=dq)

(and obviously adjusting the result accordingly)

@seberg

seberg commented Jun 10, 2026

Copy link
Copy Markdown
Member

This needs the quaddtype test helper from gh-31574. That looks like the reason for the CI failures here (it's very cryptic, but quaddtype had some bugs that caused cryptic crazy issues :)).

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.

3 participants