Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ def rescale(self, units=None, dtype=None):
'Unable to convert between units of "%s" and "%s"'
%(from_u._dimensionality, to_u._dimensionality)
)
return Quantity(cf*self.magnitude, to_u, dtype=dtype)
new_magnitude = cf*self.magnitude
dtype = np.result_type(dtype, new_magnitude)
return Quantity(new_magnitude, to_u, dtype=dtype)

def rescale_preferred(self):
"""
Expand Down
1 change: 1 addition & 0 deletions quantities/tests/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import numpy as np
from .. import units as pq
from .. import quantity
from .common import TestCase
Expand Down
13 changes: 10 additions & 3 deletions quantities/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_argmin(self):
def test_nanargmax(self):
q = np.append(self.q, np.nan) * self.q.units
self.assertEqual(self.q.nanargmin(), 0)

def test_ptp(self):
self.methodWithOut('ptp', 3 * pq.m)
self.methodWithOut('ptp', [2, 2] * pq.m, axis=0)
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_mean(self):
self.methodWithOut('mean', [1.5, 3.5] * pq.m, axis=1)

def test_nanmean(self):
import numpy as np
import numpy as np
q = [[1,2], [3,4], [np.nan,np.nan]] * pq.m
self.assertQuantityEqual(q.nanmean(), self.q.mean())

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

def test_nanstd(self):
import numpy as np
import numpy as np
q0 = [[1,2], [3,4]] * pq.m
q1 = [[1,2], [3,4], [np.nan,np.nan]] * pq.m
self.assertQuantityEqual(q0.std(), q1.nanstd())
Expand Down Expand Up @@ -345,3 +345,10 @@ def test_setitem (self):
def test_iterator(self):
for q in self.q.flatten():
self.assertQuantityEqual(q.units, pq.m)

def test_rescale_integer_argument(self):
from .. import Quantity
self.assertQuantityEqual(
Quantity(10, pq.deg).rescale(pq.rad),
np.pi/18*pq.rad
)