-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Description
Describe the issue:
The changes in scalar casting of 2.0 seem to affect NumPy arrays and masked-arrays differently for in-place operations, resulting in unexpected errors for masked-arrays. See example below (which only occurs in NumPy 2.X, but not on 1.26.4).
Maybe linked to #7991 and #27029.
EDIT: Also have cases with the same error happening not in-place.
Reproduce the code example:
import numpy as np
test = np.array([1, 2], dtype="uint8")
test += 5
test2 = np.ma.masked_array([1, 2], dtype="uint8")
test2 += 5
Error message:
Traceback (most recent call last):
File "/home/atom/miniconda3/envs/geoutils-dev/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-14912cbe9744>", line 1, in <module>
test2 += 5
File "/home/atom/miniconda3/envs/geoutils-dev/lib/python3.11/site-packages/numpy/ma/core.py", line 4422, in __iadd__
self._data.__iadd__(other_data)
numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add' output from dtype('int64') to dtype('uint8') with casting rule 'same_kind'
Python and NumPy Versions:
2.1.2
3.11.10 | packaged by conda-forge | (main, Oct 16 2024, 01:27:36) [GCC 13.3.0]
Runtime Environment:
[{'numpy_version': '2.1.2',
'python': '3.11.10 | packaged by conda-forge | (main, Oct 16 2024, 01:27:36) '
'[GCC 13.3.0]',
'uname': uname_result(system='Linux', node='pop-os', release='6.8.0-76060800daily20240311-generic', version='#202403110203171139393022.04~331756a SMP PREEMPT_DYNAMIC Mon M', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}},
{'architecture': 'Haswell',
'filepath': '/home/atom/miniconda3/envs/geoutils-dev/lib/libopenblasp-r0.3.28.so',
'internal_api': 'openblas',
'num_threads': 20,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.28'}]
Context for the issue:
I work with classes composed of NumPy masked-arrays stored in a .data
attribute (a bit like for Xarray .data
, except that it is a masked-array). Not being able to update those with simple in-place manipulation is quite limiting.