2

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?

ti7
  • 16,375
  • 6
  • 40
  • 68
Dunbur
  • 23
  • 5
  • added [pint] tag as it's a very similar library – ti7 Jun 01 '23 at 21:36
  • In 1.24, `stack` now calls `concatenate` with `concatenate(expanded_arrays, axis=axis, out=out,dtype=dtype, casting=casting)`. If `astropy` substitutes its own version of `concatenate`, that would account for the error message. – hpaulj Jun 01 '23 at 22:41
  • 1
    new bug issue for astropy on this topic https://github.com/astropy/astropy/issues/14210. Claims it was resolved in new enough astropy – hpaulj Jun 01 '23 at 22:43

2 Answers2

1

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.

  • Thanks! I have astropy 5.1. I had some issues with dependencies for poliastro. I will explore if that or some other circumstances cause me to have 5.1 instead of 5.3. – Dunbur Jun 01 '23 at 23:45
0

Had the same message/issue - Conda wouldn't update astropy to 5.3.1 using "conda update astropy" (kept giving me v5.1). Ended up doing a pip install. Even with astropy 5.3.1 it still wouldn't work with numpy 1.24.3. Had to downgrade to numpy 1.23.5. doing "conda install numpy=1.23.5". Now all is well.