Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Silencing errors of filled_value's casting in array_finalize in MaskedArray #27596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,29 @@ def _check_fill_value(fill_value, ndtype):
raise TypeError(err_msg % (fill_value, ndtype)) from e
return np.array(fill_value)

def _force_fill_value_cast(fill_value, ndtype, fallback=None):
"""
Wraps `_check_fill_value` to cast the given `fill_value` into the specified dtype.

If the `fill_value` cannot be cast into the given dtype, it returns the provided
fallback value. The result is always a 0-dimensional array if the casting is
successful.

Parameters:
fill_value: The value to be cast.
ndtype: The target data type for casting.
fallback: The value to return if casting fails
(default is None, which will return default value).

Returns:
A 0-dimensional array if casting is successful; otherwise, the fallback value.
"""
try:
result = _check_fill_value(fill_value, ndtype)
except Exception:
result = fallback
return result


def set_fill_value(a, fill_value):
"""
Expand Down Expand Up @@ -3124,7 +3147,8 @@ def __array_finalize__(self, obj):

# Finalize the fill_value
if self._fill_value is not None:
self._fill_value = _check_fill_value(self._fill_value, self.dtype)
self._fill_value = _force_fill_value_cast(self._fill_value,
self.dtype)
elif self.dtype.names is not None:
# Finalize the default fill_value for structured arrays
self._fill_value = _check_fill_value(None, self.dtype)
Expand Down Expand Up @@ -4251,11 +4275,7 @@ def _comparison(self, other, compare):
# Cast fill value to np.bool if needed. If it cannot be cast, the
# default boolean fill value is used.
if check._fill_value is not None:
try:
fill = _check_fill_value(check._fill_value, np.bool)
except (TypeError, ValueError):
fill = _check_fill_value(None, np.bool)
check._fill_value = fill
check._fill_value = _force_fill_value_cast(check._fill_value, np.bool)

return check

Expand Down
15 changes: 15 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,21 @@ def test_ndarray_mask(self):
assert_equal(test.mask, control.mask)
assert_(not isinstance(test.mask, MaskedArray))

def test_vectorize_with_type_casting(self):
# TypeError may be raised with return dtype different to input dtype.
# See issue 27165
f = np.vectorize(lambda c: ord(c) if c else -1, otypes=[int])

a = np.ma.masked_all(1, str)
x = a.fill_value # Assign default fill_value
assert_equal(f(a), np.ma.masked_all(1, np.int64))

a = np.ma.masked_array([""], True)
x = a.fill_value # Assign default fill_value
assert_equal(f(a), np.ma.masked_all(1, np.int64))
a = np.ma.masked_array([""], True, fill_value="?")
assert_equal(f(a), np.ma.masked_all(1, np.int64))

def test_treatment_of_NotImplemented(self):
# Check that NotImplemented is returned at appropriate places

Expand Down
Loading