Skip to content

Commit abefd96

Browse files
committed
Fix python-quantitiesgh-215, dtype conversion in rescale
1 parent 9212209 commit abefd96

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎quantities/quantity.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ def rescale(self, units=None, dtype=None):
219219
'Unable to convert between units of "%s" and "%s"'
220220
%(from_u._dimensionality, to_u._dimensionality)
221221
)
222-
return Quantity(cf*self.magnitude, to_u, dtype=dtype)
222+
new_magnitude = cf*self.magnitude
223+
dtype = np.result_type(dtype, new_magnitude)
224+
return Quantity(new_magnitude, to_u, dtype=dtype)
223225

224226
def rescale_preferred(self):
225227
"""

‎quantities/tests/test_conversion.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import numpy as np
23
from .. import units as pq
34
from .. import quantity
45
from .common import TestCase
@@ -15,15 +16,15 @@ def test_inplace_conversion(self):
1516
def test_rescale(self):
1617
for u in ('ft', 'feet', pq.ft):
1718
self.assertQuantityEqual((10*pq.m).rescale(u), 32.80839895 * pq.ft)
18-
19+
1920
def test_rescale_preferred(self):
2021
quantity.PREFERRED = [pq.mV, pq.pA]
2122
q = 10*pq.V
2223
self.assertQuantityEqual(q.rescale_preferred(), q.rescale(pq.mV))
2324
q = 5*pq.A
2425
self.assertQuantityEqual(q.rescale_preferred(), q.rescale(pq.pA))
2526
quantity.PREFERRED = []
26-
27+
2728
def test_rescale_preferred_failure(self):
2829
quantity.PREFERRED = [pq.pA]
2930
q = 10*pq.V
@@ -34,15 +35,15 @@ def test_rescale_preferred_failure(self):
3435
else:
3536
self.assertTrue(False)
3637
quantity.PREFERRED = []
37-
38+
3839
def test_rescale_noargs(self):
3940
quantity.PREFERRED = [pq.mV, pq.pA]
4041
q = 10*pq.V
4142
self.assertQuantityEqual(q.rescale(), q.rescale(pq.mV))
4243
q = 5*pq.A
4344
self.assertQuantityEqual(q.rescale(), q.rescale(pq.pA))
4445
quantity.PREFERRED = []
45-
46+
4647
def test_rescale_noargs_failure(self):
4748
quantity.PREFERRED = [pq.pA]
4849
q = 10*pq.V

‎quantities/tests/test_methods.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_argmin(self):
188188
def test_nanargmax(self):
189189
q = np.append(self.q, np.nan) * self.q.units
190190
self.assertEqual(self.q.nanargmin(), 0)
191-
191+
192192
def test_ptp(self):
193193
self.methodWithOut('ptp', 3 * pq.m)
194194
self.methodWithOut('ptp', [2, 2] * pq.m, axis=0)
@@ -248,7 +248,7 @@ def test_mean(self):
248248
self.methodWithOut('mean', [1.5, 3.5] * pq.m, axis=1)
249249

250250
def test_nanmean(self):
251-
import numpy as np
251+
import numpy as np
252252
q = [[1,2], [3,4], [np.nan,np.nan]] * pq.m
253253
self.assertQuantityEqual(q.nanmean(), self.q.mean())
254254

@@ -263,7 +263,7 @@ def test_std(self):
263263
self.methodWithOut('std', [0.5, 0.5] * pq.m, axis=1)
264264

265265
def test_nanstd(self):
266-
import numpy as np
266+
import numpy as np
267267
q0 = [[1,2], [3,4]] * pq.m
268268
q1 = [[1,2], [3,4], [np.nan,np.nan]] * pq.m
269269
self.assertQuantityEqual(q0.std(), q1.nanstd())
@@ -345,3 +345,10 @@ def test_setitem (self):
345345
def test_iterator(self):
346346
for q in self.q.flatten():
347347
self.assertQuantityEqual(q.units, pq.m)
348+
349+
def test_rescale_integer_argument(self):
350+
from .. import Quantity
351+
self.assertQuantityEqual(
352+
Quantity(10, pq.deg).rescale(pq.rad),
353+
np.pi/18*pq.rad
354+
)

0 commit comments

Comments
 (0)