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
5 changes: 4 additions & 1 deletion quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ def tolist(self):
#first get a dummy array from the ndarray method
work_list = self.magnitude.tolist()
#now go through and replace all numbers with the appropriate Quantity
self._tolist(work_list)
if isinstance(work_list, list):
self._tolist(work_list)
else:
work_list = Quantity(work_list, self.dimensionality)
return work_list

def _tolist(self, work_list):
Expand Down
2 changes: 2 additions & 0 deletions quantities/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def setUp(self):

def test_tolist(self):
self.assertEqual(self.q.tolist(), [[1*pq.m, 2*pq.m], [3*pq.m, 4*pq.m]])
q_singleton = 1 * pq.m
self.assertEqual(q_singleton.tolist(), q_singleton)

def test_sum(self):
self.assertQuantityEqual(self.q.sum(), 10*pq.m)
Expand Down
16 changes: 6 additions & 10 deletions quantities/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,13 @@ def test_ceil(self):
[-1., -1., -0., 1., 2., 2., 2.] * pq.m
)

@unittest.expectedFailure
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change and the change below needed? It doesn't seem to be related to the bug fix in the tolist mehtod.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thisch The numpy .fix method is something that we would like to behave the same in numpy and in quantities (i.e. it should round towards zero). It used to not work with quantities, which is why the expected failure was there. But it works now, because of changes in numpy I think (though it could be the result of my changes)? So I removed the expected failure because it now passes as it should.

def test_fix(self):
try:
self.assertQuantityEqual(np.fix(3.14 * pq.degF), 3.0 * pq.degF)
self.assertQuantityEqual(np.fix(3.0 * pq.degF), 3.0 * pq.degF)
self.assertQuantityEqual(
np.fix([2.1, 2.9, -2.1, -2.9] * pq.degF),
[2., 2., -2., -2.] * pq.degF
)
except ValueError as e:
raise self.failureException(e)
self.assertQuantityEqual(np.fix(3.14 * pq.degF), 3.0 * pq.degF)
self.assertQuantityEqual(np.fix(3.0 * pq.degF), 3.0 * pq.degF)
self.assertQuantityEqual(
np.fix([2.1, 2.9, -2.1, -2.9] * pq.degF),
[2., 2., -2., -2.] * pq.degF
)

def test_exp(self):
self.assertQuantityEqual(np.exp(1*pq.dimensionless), np.e)
Expand Down