2

I am working with the quantities module of python, and I want to concatenate two quantiy arrays. If I use numpy.concatenate(), I loose the unit from quantities. The quantities package itself does not provide a .concatenate() method for its arrays. How to avoid this?

Example:

import scipy as sp
import quantities as pq
a = [1,2,3]*pq.V
b = [4,5,6]*pq.V
sp.concatenate([a,b])

returns

array([1., 2., 3., 4., 5., 6.]) * dimensionless
grg rsr
  • 558
  • 5
  • 13
  • `np.concatenate` only knows about `numpy` arrays; it turns inputs into arrays, and returns an array. If `quantities` doesn't provide that kind of function, then most likely you'll have to added the dimensions back on yourself. That's probably the case with most `numpy` functions - they won't return as `quantities` object unless `quantities` provides a relevant method that it can deligate to. – hpaulj May 10 '18 at 16:13
  • this is what I would have expected, naively, and it is not the case. The return value of `sp.concatenate([a,b])` is, as you can see above, a `Quantity` of unit `dimensionless`. So I guess something in `numpy` removes the dimension because it doesn't handle it, and the object defaults to `dimensionless`. If that is possible, I thought maybe there is a way to prevent this from happening ... – grg rsr May 11 '18 at 16:42
  • It looks like `a` is a subclass of `ndarray`, and `np.concatenate` is preserving subclass. But it still doesn't 'know' enough to pass the dimensions through, or to resolve any conflicts. Look at `np.ma.concatenate` to see how a subclass preserves attributes (e.g. the mask) during concatenation. If `quantities` hasn't implemented that kind of action, you have to take care of it yourself. – hpaulj May 11 '18 at 16:59

0 Answers0