BUG: quantile fails with out=output when casting is necessary
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 32.8k
- Forks
- 12.8k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 197
Description
Describe the issue:
When out is not None and it is of a different type than float or different from the type the quantile() call would return as a return parameter, quantile() fails (see error message below).
The documented numpy.quantile functionality specifies that casting should be done if necessary:
out ndarray, optional
Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary.
Reproduce the code example:
import numpy as np
qs_arr = np.array([0.0, 0.5, 1.0])
q_out = np.zeros(3, dtype=int)
np.quantile(np.arange(4, dtype=float), qs_arr, out=q_out) # fails with error message below
q_outf = np.zeros(3, dtype=float)
np.quantile(np.arange(4, dtype=float), qs_arr, out=q_outf) # works fine
array([0. , 1.5, 3. ])
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 180, in quantile
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4412, in quantile
return _quantile_unchecked(
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4424, in _quantile_unchecked
r, k = _ureduce(a,
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 3725, in _ureduce
r = func(a, **kwargs)
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4593, in _quantile_ureduce_func
result = _quantile(arr,
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4710, in _quantile
result = _lerp(previous,
File "miniconda3/envs/dev_22_12_11_4_3_8/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4529, in _lerp
lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out))
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
NumPy/Python version information:
1.23.3 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)
[GCC 10.3.0]
Context for the issue:
No response
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 in numpy/lib/function_base.py around quantile, _quantile_ureduce_func, and _quantile, then run the reported example with an integer out array. Check the existing quantile behavior and tests before adding regression coverage; done means valid out arrays receive the result with the documented casting behavior without the UFuncTypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100