numpyastropypint

numpy stack not working for astropy quantities in numpy 1.23


The following works in numpy 1.23.5 but not in 1.24.3:

from astropy import units as u
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
np.stack([a,b]*u.m)

With numpy 1.24.3, I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 200, in stack
  File "~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py", line 1683, in __array_function__
    return super().__array_function__(function, types, args, kwargs)
  File "~/opt/anaconda3/lib/python3.9/site-packages/numpy/core/shape_base.py", line 471, in stack
    return _nx.concatenate(expanded_arrays, axis=axis, out=out,
  File "<__array_function__ internals>", line 200, in concatenate
  File "~/opt/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py", line 1688, in __array_function__
    args, kwargs, unit, out = function_helper(*args, **kwargs)
TypeError: concatenate() got an unexpected keyword argument 'dtype'

While with numpy 1.23.5, I get:

<Quantity [[[1., 2.],
            [3., 4.]],

           [[5., 6.],
            [7., 8.]]] m>

Am I doing something wrong or has been a bug introduced in numpy 1.24 or does astropy units have to be updated to work with the new numpy version?


Solution

  • It's work for me with setup:

    Python 3.9.12 (main, Apr 5 2022, 01:53:17) [Clang 12.0.0 ]
    Numpy 1.24.3
    Astropy 5.3

    In version NumPy 1.24.3 developers added new arguments for function stack: casting and dtype. As I see in provided error message, astropy got unexpected keyword dtype and that may be an issue here. I assume that your version of astropy - 5.1 and recommend upgrade to 5.3.