|
49 | 49 | array, |
50 | 50 | asarray, |
51 | 51 | choose, |
| 52 | + common_fill_value, |
52 | 53 | concatenate, |
53 | 54 | conjugate, |
54 | 55 | cos, |
|
89 | 90 | masked_less, |
90 | 91 | masked_less_equal, |
91 | 92 | masked_not_equal, |
| 93 | + masked_object, |
92 | 94 | masked_outside, |
93 | 95 | masked_print_option, |
94 | 96 | masked_values, |
@@ -2691,6 +2693,37 @@ def test_fillvalue_bytes_or_str(self): |
2691 | 2693 | assert_equal(a["f0"].fill_value, default_fill_value(b"spam")) |
2692 | 2694 | assert_equal(a["f1"].fill_value, default_fill_value("eggs")) |
2693 | 2695 |
|
| 2696 | + def test_common_fill_value(self): |
| 2697 | + # Test with matching fill value, across different dtypes and shapes. |
| 2698 | + a = array([1, 2, 3], dtype=int, fill_value=10) |
| 2699 | + b = array([[4, 5], [6, 7]], dtype=float, fill_value=10) |
| 2700 | + assert_equal(common_fill_value(a, b), 10) |
| 2701 | + |
| 2702 | + # Test with non-matching fill value. |
| 2703 | + b.fill_value = -10 |
| 2704 | + assert common_fill_value(a, b) is None |
| 2705 | + |
| 2706 | + @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm") |
| 2707 | + def test_fillvalue_reset_on_lossy_float_cast(self): |
| 2708 | + # gh-28255 |
| 2709 | + a = arange(9.0) |
| 2710 | + untouched = np.ones_like(a, dtype="int64") |
| 2711 | + a.fill_value # materialise the default fill_value |
| 2712 | + with warnings.catch_warnings(): |
| 2713 | + warnings.simplefilter("error") |
| 2714 | + touched = np.ones_like(a, dtype="int64") |
| 2715 | + assert_equal(touched.fill_value, untouched.fill_value) |
| 2716 | + assert_equal(touched.fill_value, default_fill_value(touched.dtype)) |
| 2717 | + |
| 2718 | + def test_fillvalue_kept_on_exact_float_cast(self): |
| 2719 | + a = array([1.0, 2.0], mask=[0, 1], fill_value=5.0) |
| 2720 | + assert_equal(np.ones_like(a, dtype="int64").fill_value, 5) |
| 2721 | + |
| 2722 | + def test_fillvalue_setter_still_raises(self): |
| 2723 | + a = array([1, 2], dtype="int64") |
| 2724 | + with pytest.raises(TypeError): |
| 2725 | + a.fill_value = 1e20 |
| 2726 | + |
2694 | 2727 |
|
2695 | 2728 | class TestUfuncs: |
2696 | 2729 | # Test class for the application of ufuncs on MaskedArrays. |
@@ -4792,10 +4825,12 @@ def test_where(self): |
4792 | 4825 | tmp[(xm <= 2).filled(True)] = True |
4793 | 4826 | assert_equal(d._mask, tmp) |
4794 | 4827 |
|
4795 | | - with np.errstate(invalid="warn"): |
4796 | | - # The fill value is 1e20, it cannot be converted to `int`: |
4797 | | - with pytest.warns(RuntimeWarning, match="invalid value"): |
4798 | | - ixm = xm.astype(int) |
| 4828 | + # The fill value is 1e20, it cannot be converted to `int`, so the |
| 4829 | + # cast falls back to the default fill_value (gh-28255): |
| 4830 | + with warnings.catch_warnings(): |
| 4831 | + warnings.simplefilter("error") |
| 4832 | + ixm = xm.astype(int) |
| 4833 | + assert_equal(ixm.fill_value, default_fill_value(ixm.dtype)) |
4799 | 4834 | d = where(ixm > 2, ixm, masked) |
4800 | 4835 | assert_equal(d, [-9, -9, -9, -9, -9, 4, -9, -9, 10, -9, -9, 3]) |
4801 | 4836 | assert_equal(d.dtype, ixm.dtype) |
@@ -5747,8 +5782,22 @@ def test_attributes_readonly(self): |
5747 | 5782 |
|
5748 | 5783 |
|
5749 | 5784 | class TestMaskedWhereAliases: |
5750 | | - |
5751 | | - # TODO: Test masked_object, masked_equal, ... |
| 5785 | + def test_masked_object(self): |
| 5786 | + food = np.array(['green_eggs', 'ham'], dtype=object) |
| 5787 | + res = masked_object(food, 'green_eggs') |
| 5788 | + assert_equal(res.mask, [True, False]) |
| 5789 | + assert_(res[0] is masked) |
| 5790 | + assert_equal(res.fill_value, 'green_eggs') |
| 5791 | + |
| 5792 | + res = masked_object(food, 'cheese') |
| 5793 | + assert_(res.mask is nomask) |
| 5794 | + |
| 5795 | + res = masked_object(food, 'cheese', shrink=False) |
| 5796 | + assert_equal(res.mask, [False, False]) |
| 5797 | + |
| 5798 | + xm = array(['a', 'b', 'c'], mask=[1, 0, 0], dtype=object) |
| 5799 | + res = masked_object(xm, 'c') |
| 5800 | + assert_equal(res.mask, [True, False, True]) |
5752 | 5801 |
|
5753 | 5802 | def test_masked_values(self): |
5754 | 5803 | res = masked_values(np.array([-32768.0]), np.int16(-32768)) |
|
0 commit comments