Skip to content

BUG: allow MaskedArray.fill_value be a string when dtype=StringDType #29423

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 7 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
TST: add tests for StringDType MaskedArray use
  • Loading branch information
ngoldbaum authored and phdparedes committed Jul 24, 2025
commit 2c281c20cc420591f278cdcee8e29b93ba4a9876
19 changes: 14 additions & 5 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ def test_eq_ne_structured_extra(self):
el_by_el = [m1[name] != m2[name] for name in dt.names]
assert_equal(array(el_by_el, dtype=bool).any(), ne_expected)

@pytest.mark.parametrize('dt', ['S', 'U'])
@pytest.mark.parametrize('dt', ['S', 'U', 'T'])
@pytest.mark.parametrize('fill', [None, 'A'])
def test_eq_for_strings(self, dt, fill):
# Test the equality of structured arrays
Expand Down Expand Up @@ -1839,7 +1839,7 @@ def test_eq_for_strings(self, dt, fill):
assert_equal(test.mask, [True, False])
assert_(test.fill_value == True)

@pytest.mark.parametrize('dt', ['S', 'U'])
@pytest.mark.parametrize('dt', ['S', 'U', 'T'])
@pytest.mark.parametrize('fill', [None, 'A'])
def test_ne_for_strings(self, dt, fill):
# Test the equality of structured arrays
Expand Down Expand Up @@ -1989,17 +1989,26 @@ def test_comparisons_for_numeric(self, op, dt1, dt2, fill):
assert_equal(test.mask, [True, False])
assert_(test.fill_value == True)

@pytest.mark.parametrize('dt', ['S', 'U', 'T'])
@pytest.mark.parametrize('op',
[operator.le, operator.lt, operator.ge, operator.gt])
@pytest.mark.parametrize('fill', [None, "N/A"])
def test_comparisons_strings(self, op, fill):
def test_comparisons_strings(self, dt, op, fill):
# See gh-21770, mask propagation is broken for strings (and some other
# cases) so we explicitly test strings here.
# In principle only == and != may need special handling...
ma1 = masked_array(["a", "b", "cde"], mask=[0, 1, 0], fill_value=fill)
ma2 = masked_array(["cde", "b", "a"], mask=[0, 1, 0], fill_value=fill)
ma1 = masked_array(["a", "b", "cde"], mask=[0, 1, 0], fill_value=fill, dtype=dt)
ma2 = masked_array(["cde", "b", "a"], mask=[0, 1, 0], fill_value=fill, dtype=dt)
assert_equal(op(ma1, ma2)._data, op(ma1._data, ma2._data))

if isinstance(fill, str):
fill = np.array(fill, dtype=dt)

ma1 = masked_array(["a", "b", "cde"], mask=[0, 1, 0], fill_value=fill, dtype=dt)
ma2 = masked_array(["cde", "b", "a"], mask=[0, 1, 0], fill_value=fill, dtype=dt)
assert_equal(op(ma1, ma2)._data, op(ma1._data, ma2._data))


Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

to appease the linter

Copy link
Author

Choose a reason for hiding this comment

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

I'm adding test functions for test_core.py now.

@pytest.mark.filterwarnings("ignore:.*Comparison to `None`.*:FutureWarning")
def test_eq_with_None(self):
# Really, comparisons with None should not be done, but check them
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy