Skip to content

Commit

Permalink
Merge #1131
Browse files Browse the repository at this point in the history
1131: fix reduce dimensions when previous dimension was zeroed out r=hgrecco a=bloer

…1058

- [1058] Closes # (insert issue number)
- [+] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [+] The change is fully covered by automated unit tests
- [+] Documented in docs/ as appropriate
- [+] Added an entry to the CHANGES file


Co-authored-by: Ben Loer <[email protected]>
Co-authored-by: Hernan Grecco <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2020
2 parents 91194f3 + 92716d2 commit f9078cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ Pint Changelog
units (Issue #1145)
- Implement Dask collection interface to support Pint Quantity wrapped Dask arrays.
- Started automatically testing examples in the documentation
- Fixed an exception generated when reducing dimensions with three or more
units of the same type
- Fixed right operand power for dimensionless Quantity to reflect numpy behavior. (Issue #1136)
- Eliminated warning when setting a masked value on an underlying MaskedArray.
- Add `sort` option to `formatting.formatter` to permit disabling sorting of component units in format string


0.14 (2020-07-01)
-----------------

Expand Down
7 changes: 5 additions & 2 deletions pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,8 @@ def to_base_units(self):

def ito_reduced_units(self):
"""Return Quantity scaled in place to reduced units, i.e. one unit per
dimension. This will not reduce compound units (intentionally), nor
can it make use of contexts at this time.
dimension. This will not reduce compound units (e.g., 'J/kg' will not
be reduced to m**2/s**2), nor can it make use of contexts at this time.
"""

# shortcuts in case we're dimensionless or only a single unit
Expand All @@ -716,6 +716,9 @@ def ito_reduced_units(self):
# loop through individual units and compare to each other unit
# can we do better than a nested loop here?
for unit1, exp in self._units.items():
# make sure it wasn't already reduced to zero exponent on prior pass
if unit1 not in newunits:
continue
for unit2 in newunits:
if unit1 != unit2:
power = self._REGISTRY._get_dimensionality_ratio(unit1, unit2)
Expand Down
7 changes: 7 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ def test_issue973(self):
assert isinstance(q1, ureg.Quantity)
assert len(q0) == len(q1) == 0

def test_issue1058(self):
""" verify that auto-reducing quantities with three or more units
of same base type succeeds """
q = 1 * ureg.mg / ureg.g / ureg.kg
q.ito_reduced_units()
self.assertIsInstance(q, ureg.Quantity)

def test_issue1062_issue1097(self):
# Must not be used by any other tests
assert "nanometer" not in ureg._units
Expand Down

0 comments on commit f9078cf

Please sign in to comment.