diff options
| author | Hernan Grecco <hernan.grecco@gmail.com> | 2023-04-29 10:01:59 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-29 10:01:59 -0300 |
| commit | 9c7648c0bcc3d57f2ec4dde2e2df668e672b707e (patch) | |
| tree | 0632766da14c3c3cfd248a81128d38e3af891cd8 | |
| parent | c7f88683000c2c7b23f7d5148845078e9aa3e2f5 (diff) | |
| parent | 6ec28e21c4a2961a0a3421516f8874d27f941c0f (diff) | |
| download | pint-9c7648c0bcc3d57f2ec4dde2e2df668e672b707e.tar.gz | |
Merge pull request #1608 from dopplershift/fix-1584
Fix setitem with a masked array with multiple items (Fixes #1584)
| -rw-r--r-- | CHANGES | 2 | ||||
| -rw-r--r-- | pint/facets/numpy/quantity.py | 7 | ||||
| -rw-r--r-- | pint/testsuite/test_numpy.py | 18 |
3 files changed, 25 insertions, 2 deletions
@@ -86,6 +86,8 @@ types for pint-pandas compatibility. (#1596) - Create NaN-value quantities of appropriate non-int-type (Issue #1570). - New documentation format and organization! - Better support for pandas and dask. +- Fix masked arrays (with multiple values) incorrectly being passed through + setitem (Issue #1584) - Add Quantity.to_preferred diff --git a/pint/facets/numpy/quantity.py b/pint/facets/numpy/quantity.py index 0d335cd..9aa55ce 100644 --- a/pint/facets/numpy/quantity.py +++ b/pint/facets/numpy/quantity.py @@ -244,7 +244,12 @@ class NumpyQuantity: def __setitem__(self, key, value): try: - if np.ma.is_masked(value) or math.isnan(value): + # If we're dealing with a masked single value or a nan, set it + if ( + isinstance(self._magnitude, np.ma.MaskedArray) + and np.ma.is_masked(value) + and getattr(value, "size", 0) == 1 + ) or math.isnan(value): self._magnitude[key] = value return except TypeError: diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 080c60a..550584f 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -927,7 +927,7 @@ class TestNumpyUnclassified(TestNumpyMethods): q[:] = 1 * self.ureg.m helpers.assert_quantity_equal(q, [[1, 1], [1, 1]] * self.ureg.m) - # check and see that dimensionless num bers work correctly + # check and see that dimensionless numbers work correctly q = [0, 1, 2, 3] * self.ureg.dimensionless q[0] = 1 helpers.assert_quantity_equal(q, np.asarray([1, 1, 2, 3])) @@ -948,6 +948,22 @@ class TestNumpyUnclassified(TestNumpyMethods): assert not w assert q.mask[0] + def test_setitem_mixed_masked(self): + masked = np.ma.array( + [ + 1, + 2, + ], + mask=[True, False], + ) + q = self.Q_(np.ones(shape=(2,)), "m") + with pytest.raises(DimensionalityError): + q[:] = masked + + masked_q = self.Q_(masked, "mm") + q[:] = masked_q + helpers.assert_quantity_equal(q, [1.0, 0.002] * self.ureg.m) + def test_iterator(self): for q, v in zip(self.q.flatten(), [1, 2, 3, 4]): assert q == v * self.ureg.m |
