Skip to content

passing a quantity array to numpy functions (e.g. meshgrid) discards units #47

Open

Description

@twmr

I don't know if this can be fixed in python-quantities or if it requires a fix in numpy itself.

In [1]: import numpy as np

In [2]: import quantities as pq

In [3]: a = np.arange(10)*pq.V

In [4]: X,Y = np.meshgrid(a,a)

In [5]: print X
[[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]]

Activity

  1. jgosmann commented on Jan 18, 2013

    @jgosmann

    I would appreciate a fix for this.

  2. jgosmann commented on Jan 18, 2013

    @jgosmann

    Same for numpy.concatenate.

  3. mforbes commented on Oct 23, 2014

    @mforbes

    And np.arange.

  4. mikofski commented on Mar 28, 2016

    @mikofski

    @ddale this example in this issue works as expected for me now, so I think this could be closed, however I did find a similar issue.

    Using np.array, np.append and np.concatenate removes units, even if they are consistent - I could see how maybe this is a feature to prevent inconsistent units. My work around is quite fast, simply, reapply the units to the result.

    import quantities as pq
    import numpy as np
    a, b = 1.23 * pq.V, 4.56 * pq.V
    print np.array([a, b])
    # [ 1.23  4.56]
    print np.append(a, b)
    # [ 1.23  4.56] dimensionless
    print np.concatenate([a.reshape((1,1)), b.reshape((1,1))])
    # [[ 1.23]
    #  [ 4.56]] dimensionless
  5. added
    bug
    on Nov 17, 2026
  6. added this to the 0.18.0 milestone on Nov 17, 2026
  7. added
    numpy-integration
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnumpy-integration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions