[BUG] `np.average` with weights fails
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24
- Forks
- 6
- Avg merge
- 5d 41m
- Merged PRs (30d)
- 3
Description
import numpy_quaddtype
x=numpy_quaddtype.QuadPrecision([1,2,3])
y=numpy_quaddtype.QuadPrecision([2,2.5,2.3])
np.average(x,weights=1/y)
fails:
File ~/miniforge3/envs/pintdev_arm/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py:587, in average(a, axis, weights, returned, keepdims)
584 else:
585 result_dtype = np.result_type(a.dtype, wgt.dtype)
--> 587 scl = wgt.sum(axis=axis, dtype=result_dtype, **keepdims_kw)
588 if np.any(scl == 0.0):
589 raise ZeroDivisionError(
590 "Weights sum to zero, can't be normalized")
File ~/miniforge3/envs/pintdev_arm/lib/python3.14/site-packages/numpy/_core/_methods.py:49, in _sum(a, axis, dtype, out, keepdims, initial, where)
47 def _sum(a, axis=None, dtype=None, out=None, keepdims=False,
48 initial=_NoValue, where=True):
---> 49 return umr_sum(a, axis, dtype, out, keepdims, initial, where)
TypeError: Cannot pass a new user DType instance to the `dtype` or `signature` arguments of ufuncs. Pass the DType class instead.
Without the weights argument this works OK.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided weighted np.average example with numpy_quaddtype. Inspect the weighted path around NumPy's average implementation and dtype handling shown in the traceback. Done means the example no longer raises the new user DType instance error while preserving the existing unweighted behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100