Skip to content

Commit f08a50a

Browse files
committed
Preserve dtype in rescale
1 parent 2eeacc1 commit f08a50a

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎quantities/quantity.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def rescale(self, units=None):
198198
"""
199199
Return a copy of the quantity converted to the specified units.
200200
If `units` is `None`, an attempt will be made to rescale the quantity
201-
to preferred units (see `rescale_preferred`).
201+
to preferred units (see `rescale_preferred`).
202202
"""
203203
if units is None:
204204
try:
@@ -217,8 +217,8 @@ def rescale(self, units=None):
217217
'Unable to convert between units of "%s" and "%s"'
218218
%(from_u._dimensionality, to_u._dimensionality)
219219
)
220-
return Quantity(cf*self.magnitude, to_u)
221-
220+
return Quantity(cf*self.magnitude, to_u, dtype=self.dtype)
221+
222222
def rescale_preferred(self):
223223
"""
224224
Return a copy of the quantity converted to the preferred units and scale.
@@ -238,7 +238,7 @@ def rescale_preferred(self):
238238
return self.rescale(preferred)
239239
raise Exception("Preferred units for '%s' (or equivalent) not specified in "
240240
"quantites.quantity.PREFERRED." % self.dimensionality)
241-
241+
242242
@with_doc(np.ndarray.astype)
243243
def astype(self, dtype=None, **kwargs):
244244
'''Scalars are returned as scalar Quantity arrays.'''

‎quantities/tests/test_uncertainty.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .. import units as pq
2+
from ..quantity import Quantity
23
from ..uncertainquantity import UncertainQuantity
34
from .common import TestCase
45
import numpy as np
@@ -31,6 +32,13 @@ def test_rescale(self):
3132
[0.32808399, 0.32808399, 0.32808399]*pq.ft
3233
)
3334

35+
seventy_km = Quantity(70, pq.km, dtype=np.float32)
36+
seven_km = Quantity(7, pq.km, dtype=np.float32)
37+
seventyish_km = UncertainQuantity(seventy_km, pq.km, seven_km, dtype=np.float32)
38+
self.assertTrue(seventyish_km.dtype == np.float32)
39+
in_meters = seventyish_km.rescale(pq.m)
40+
self.assertTrue(in_meters.dtype == seventyish_km.dtype)
41+
3442
def test_set_uncertainty(self):
3543
a = UncertainQuantity([1, 2], 'm', [.1, .2])
3644
a.uncertainty = [1., 2.]*pq.m

0 commit comments

Comments
 (0)