Specutils导入报错:TypeError: concatenate()收到意外关键字参数'dtype'
解决导入specutils时的
TypeError: concatenate() got an unexpected keyword argument 'dtype'错误 问题详情
近期在Jupyter Notebook中导入Python的specutils库时触发如下错误:
TypeError: concatenate() got an unexpected keyword argument 'dtype'
尝试安装np.concatenate添加dtype参数之前的旧版numpy,但未解决问题。完整报错堆栈如下:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/c0/bxk1h7r54tb6c3hllk8gs7nr0000gn/T/ipykernel_3886/2843005521.py in <module> ----> 1 from specutils import Spectrum1D 2 from specutils import SpectralRegion 3 from specutils.analysis import equivalent_width 4 from specutils.fitting import fit_generic_continuum ~/anaconda3/lib/python3.9/site-packages/specutils/__init__.py in <module> 21 22 # Allow loading spectrum object from top level module ---> 23 from .spectra import * # noqa 24 25 # Load the IO functions ~/anaconda3/lib/python3.9/site-packages/specutils/spectra/__init__.py in <module> 3 `~astropy.nddata.NDData`-inherited classes used for storing the spectrum data. 4 """ ----> 5 from .spectrum1d import * # noqa 6 from .spectral_region import * # noqa 7 from .spectrum_collection import * # noqa ~/anaconda3/lib/python3.9/site-packages/specutils/spectra/spectrum1d.py in <module> 13 from ..utils.wcs_utils import gwcs_from_array 14 ---> 15 from ndcube import NDCube 16 17 __all__ = ['Spectrum1D'] ~/anaconda3/lib/python3.9/site-packages/ndcube/__init__.py in <module> 28 from .global_coords import GlobalCoords 29 from .ndcollection import NDCollection ---> 30 from .ndcube import NDCube 31 from .ndcube_sequence import NDCubeSequence 32 from .version import version as __version__ ~/anaconda3/lib/python3.9/site-packages/ndcube/ndcube.py in <module> 13 try: 14 # Import sunpy coordinates if available to register the frames and WCS functions with astropy ---> 15 import sunpy.coordinates # pylint: disable=unused-import # NOQA 16 except ImportError: 17 pass ~/anaconda3/lib/python3.9/site-packages/sunpy/coordinates/__init__.py in <module> 19 """ 20 ---> 21 from . import sun 22 from .ephemeris import * 23 from .frames import * ~/anaconda3/lib/python3.9/site-packages/sunpy/coordinates/sun.py in <module> 32 from .ephemeris import get_earth 33 from .frames import HeliographicStonyhurst ---> 34 from .transformations import _SOLAR_NORTH_POLE_HCRS, _SUN_DETILT_MATRIX 35 36 __author__ = "Albert Y. Shih" ~/anaconda3/lib/python3.9/site-packages/sunpy/coordinates/transformations.py in <module> 645 646 # Calculate the rotation matrix to de-tilt the Sun's rotation axis to be parallel to the Z axis ---> 647 _SUN_DETILT_MATRIX = _rotation_matrix_reprs_to_reprs(_SOLAR_NORTH_POLE_HCRS, 648 CartesianRepresentation(0, 0, 1)) 649 ~/anaconda3/lib/python3.9/site-packages/sunpy/coordinates/transformations.py in _rotation_matrix_reprs_to_reprs(start_representation, end_representation) 595 A = start_representation.to_cartesian() 596 B = end_representation.to_cartesian() ---> 597 rotation_axis = A.cross(B) 598 rotation_angle = -np.arccos(A.dot(B) / (A.norm() * B.norm())) # negation is required 599 ~/anaconda3/lib/python3.9/site-packages/astropy/coordinates/representation.py in cross(self, other) ~/anaconda3/lib/python3.9/site-packages/astropy/coordinates/representation.py in get_xyz(self, xyz_axis) ~/anaconda3/lib/python3.9/site-packages/numpy/core/overrides.py in stack(*args, **kwargs) ~/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py in __array_function__(self, function, types, args, kwargs) 1681 # If, e.g., we are casting float to int, we want to fail if 1682 # precision is lost, but let things pass if it works. -> 1683 _value = np.array(_value, copy=False, subok=True) 1684 if not np.can_cast(_value.dtype, self.dtype): 1685 self_dtype_array = np.array(_value, self.dtype, subok=True) ~/anaconda3/lib/python3.9/site-packages/numpy/core/shape_base.py in stack(arrays, axis, out, dtype, casting) 469 def _block_format_index(index): 470 """ ---> 471 Convert a list of indices ``[0, 1, 2]`` into ``"arrays[0][1][2]"``. 472 """ 473 idx_str = ''.join('[{}]'.format(i) for i in index if i is not None) ~/anaconda3/lib/python3.9/site-packages/numpy/core/overrides.py in concatenate(*args, **kwargs) ~/anaconda3/lib/python3.9/site-packages/astropy/units/quantity.py in __array_function__(self, function, types, args, kwargs) 1686 if not np.all((self_dtype_array == _value) | np.isnan(_value)): 1687 raise TypeError( -> 1688 "cannot convert value type to array type without precision loss" 1689 ) 1690 TypeError: concatenate() got an unexpected keyword argument 'dtype'
解决方法
检查并同步依赖版本
该错误源于specutils、astropy、sunpy、numpy等库的版本不兼容,单独降级numpy无法解决问题。先查看当前环境的依赖版本:pip show specutils astropy sunpy ndcube numpy根据specutils官方文档的版本要求,安装互相兼容的依赖组合。例如specutils 1.x通常需要astropy 4.0+、numpy 1.21+,且sunpy版本需与astropy匹配。
重新安装完整依赖链
卸载冲突库后,使用specutils的完整安装包自动拉取兼容依赖:pip uninstall -y specutils astropy sunpy ndcube numpy pip install specutils[all]使用虚拟环境隔离
若当前环境有其他项目的依赖冲突,创建独立虚拟环境:python -m venv specutils_env # Windows系统激活命令:specutils_env\Scripts\activate source specutils_env/bin/activate pip install specutils jupyter升级astropy修复参数传递问题
报错显示astropy的Quantity类调用numpy concatenate时传递了不支持的dtype参数,升级astropy到兼容版本可解决:pip install --upgrade astropy
内容的提问来源于stack exchange,提问作者perii
相关产品推荐
相关产品推荐

