(self)
| 4031 | assert expected_attrs == actual["attrs"] |
| 4032 | |
| 4033 | def test_to_masked_array(self) -> None: |
| 4034 | rs = np.random.default_rng(44) |
| 4035 | x = rs.random(size=(10, 20)) |
| 4036 | x_masked = np.ma.masked_where(x < 0.5, x) |
| 4037 | da = DataArray(x_masked) |
| 4038 | |
| 4039 | # Test round trip |
| 4040 | x_masked_2 = da.to_masked_array() |
| 4041 | da_2 = DataArray(x_masked_2) |
| 4042 | assert_array_equal(x_masked, x_masked_2) |
| 4043 | assert_equal(da, da_2) |
| 4044 | |
| 4045 | da_masked_array = da.to_masked_array(copy=True) |
| 4046 | assert isinstance(da_masked_array, np.ma.MaskedArray) |
| 4047 | # Test masks |
| 4048 | assert_array_equal(da_masked_array.mask, x_masked.mask) |
| 4049 | # Test that mask is unpacked correctly |
| 4050 | assert_array_equal(da.values, x_masked.filled(np.nan)) |
| 4051 | # Test that the underlying data (including nans) hasn't changed |
| 4052 | assert_array_equal(da_masked_array, x_masked.filled(np.nan)) |
| 4053 | |
| 4054 | # Test that copy=False gives access to values |
| 4055 | masked_array = da.to_masked_array(copy=False) |
| 4056 | masked_array[0, 0] = 10.0 |
| 4057 | assert masked_array[0, 0] == 10.0 |
| 4058 | assert da[0, 0].values == 10.0 |
| 4059 | assert masked_array.base is da.values |
| 4060 | assert isinstance(masked_array, np.ma.MaskedArray) |
| 4061 | |
| 4062 | # Test with some odd arrays |
| 4063 | for v in [4, np.nan, True, "4", "four"]: |
| 4064 | da = DataArray(v) |
| 4065 | ma = da.to_masked_array() |
| 4066 | assert isinstance(ma, np.ma.MaskedArray) |
| 4067 | |
| 4068 | # Fix GH issue 684 - masked arrays mask should be an array not a scalar |
| 4069 | N = 4 |
| 4070 | v = range(N) |
| 4071 | da = DataArray(v) |
| 4072 | ma = da.to_masked_array() |
| 4073 | assert isinstance(ma.mask, np.ndarray) and len(ma.mask) == N |
| 4074 | |
| 4075 | def test_to_dataset_whole(self) -> None: |
| 4076 | unnamed = DataArray([1, 2], dims="x") |
nothing calls this directly
no test coverage detected