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

Skip to content

BUG: multifield-view of MaskedArray gets bad fill_value #12408

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

Merged
merged 1 commit into from
Nov 25, 2018
Merged
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
14 changes: 8 additions & 6 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,15 @@ def _check_fill_value(fill_value, ndtype):

"""
ndtype = np.dtype(ndtype)
fields = ndtype.fields
if fill_value is None:
fill_value = default_fill_value(ndtype)
elif fields:
fdtype = [(_[0], _[1]) for _ in ndtype.descr]
elif ndtype.names is not None:
if isinstance(fill_value, (ndarray, np.void)):
try:
fill_value = np.array(fill_value, copy=False, dtype=fdtype)
fill_value = np.array(fill_value, copy=False, dtype=ndtype)
except ValueError:
err_msg = "Unable to transform %s to dtype %s"
raise ValueError(err_msg % (fill_value, fdtype))
raise ValueError(err_msg % (fill_value, ndtype))
else:
fill_value = np.asarray(fill_value, dtype=object)
fill_value = np.array(_recursive_set_fill_value(fill_value, ndtype),
Expand Down Expand Up @@ -780,6 +778,10 @@ def fix_invalid(a, mask=nomask, copy=True, fill_value=None):
a._data[invalid] = fill_value
return a

def is_string_or_list_of_strings(val):
return (isinstance(val, basestring) or
(isinstance(val, list) and
builtins.all(isinstance(s, basestring) for s in val)))

###############################################################################
# Ufuncs #
Expand Down Expand Up @@ -3245,7 +3247,7 @@ def _scalar_heuristic(arr, elem):
# Inherit attributes from self
dout._update_from(self)
# Check the fill_value
if isinstance(indx, basestring):
if is_string_or_list_of_strings(indx):
if self._fill_value is not None:
dout._fill_value = self._fill_value[indx]

Expand Down
11 changes: 11 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,17 @@ def test_fillvalue(self):
assert_equal(x.fill_value, 999.)
assert_equal(x._fill_value, np.array(999.))

def test_subarray_fillvalue(self):
# gh-10483 test multi-field index fill value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having a separate test here

fields = array([(1, 1, 1)],
dtype=[('i', int), ('s', '|S8'), ('f', float)])
with suppress_warnings() as sup:
sup.filter(FutureWarning, "Numpy has detected")
subfields = fields[['i', 'f']]
assert_equal(tuple(subfields.fill_value), (999999, 1.e+20))
# test comparison does not raise:
subfields[1:] == subfields[:-1]

def test_fillvalue_exotic_dtype(self):
# Tests yet more exotic flexible dtypes
_check_fill_value = np.ma.core._check_fill_value
Expand Down