BUG: np.compress casting fails from narrower to wider type
Author: pijyoiCreated Jun 6, 2022Updated Sep 30, 2026
Labels00 - Bugcomponent: numpy._coresustain-2026
Describe the issue:
When the output type is wider than the input type, a TypeError is raised. (Expected no error) Conversely, when the output type is narrower than the input type, no TypeError is raised. (Expected error)
Reproduce the code example:
import numpy as np
def test(dtype_in, dtype_out):
expected = np.can_cast(dtype_in, dtype_out)
try:
ain = np.arange(10, dtype=dtype_in)
mask = ain < 5
aout = np.empty(np.count_nonzero(mask), dtype=dtype_out)
np.compress(mask, ain, out=aout)
except TypeError:
success = False
else:
success = True
print(f'compress {dtype_in} to {dtype_out}; {expected=}; {success=}')
test('f8', 'i4')
test('i4', 'f8')Error message:
compress f8 to i4; expected=False; success=True
compress i4 to f8; expected=True; success=FalseNumPy/Python version information:
1.22.4 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
Source: numpy/numpy