Skip to content

Commit 5accbf6

Browse files
gerritholljaimefrio
authored andcommitted
BUG/TST: Fix numpy#7259, do not "force scalar" for already scalar str/bytes
Fix for numpy#7259. Since commit f8f753b, fill_value was forced scalar by indexing with [()]. This works for most numpy scalars, but not for U (str) and S (bytes). This commit falls back to the pre-f8f753b version when forcing to scalar fails for fill_value of dtypes with kind U and S. Also add a test to see that this works in practice.
1 parent e986bc8 commit 5accbf6

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎numpy/ma/core.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,14 @@ def get_fill_value(self):
35303530
"""
35313531
if self._fill_value is None:
35323532
self._fill_value = _check_fill_value(None, self.dtype)
3533-
return self._fill_value[()]
3533+
3534+
# Temporary workaround to account for the fact that str and bytes
3535+
# scalars cannot be indexed with (), whereas all other numpy
3536+
# scalars can. See issues #7259 and #7267.
3537+
# The if-block can be removed after #7267 has been fixed.
3538+
if isinstance(self._fill_value, ndarray):
3539+
return self._fill_value[()]
3540+
return self._fill_value
35343541

35353542
def set_fill_value(self, value=None):
35363543
"""

‎numpy/ma/tests/test_core.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,13 @@ def test_fillvalue_in_view(self):
18191819
y = x.view(dtype=np.int32)
18201820
assert_(y.fill_value == 999999)
18211821

1822+
def test_fillvalue_bytes_or_str(self):
1823+
# Test whether fill values work as expected for structured dtypes
1824+
# containing bytes or str. See issue #7259.
1825+
a = empty(shape=(3, ), dtype="(2)3S,(2)3U")
1826+
assert_equal(a["f0"].fill_value, default_fill_value(b"spam"))
1827+
assert_equal(a["f1"].fill_value, default_fill_value("eggs"))
1828+
18221829

18231830
class TestUfuncs(TestCase):
18241831
# Test class for the application of ufuncs on MaskedArrays.

0 commit comments

Comments
 (0)