You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

高版本NumPy与Pandas兼容报错:np.bool属性缺失求解决方案

问题

当前使用Pandas 2.2.2、NumPy 1.26.4,因高版本NumPy导致无法正常导入Pandas,报错如下:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pandas as pd, seaborn as sns, sqlalchemy, plotly, numpy as np

File ~\anaconda3\lib\site-packages\pandas\__init__.py:180
    177 from pandas.io.json import _json_normalize as json_normalize
    179 from pandas.util._tester import test
--> 180 import pandas.testing
    181 import pandas.arrays
    183 # use the closest tagged version if possible

File ~\anaconda3\lib\site-packages\pandas\testing.py:5
      1 """
      2 Public testing utility functions.
      3 """
----> 5 from pandas._testing import (
      6     assert_extension_array_equal,
      7     assert_frame_equal,
      8     assert_index_equal,
      9     assert_series_equal,
     10 )
     12 __all__ = [
     13     "assert_extension_array_equal",
     14     "assert_frame_equal",
     15     "assert_series_equal",
     16     "assert_index_equal",
     17 ]

File ~\anaconda3\lib\site-packages\pandas\_testing.py:24
     16 from numpy.random import rand, randn
     18 from pandas._config.localization import (  # noqa:F401
     19     can_set_locale,
     20     get_locales,
     21     set_locale,
     22 )
---> 24 import pandas._libs.testing as _testing
     25 from pandas._typing import FilePathOrBuffer, FrameOrSeries
     26 from pandas.compat import _get_lzma_file, _import_lzma

File pandas\_libs\testing.pyx:10, in init pandas._libs.testing()

File ~\anaconda3\lib\site-packages\numpy\__init__.py:305, in __getattr__(attr)
    300     warnings.warn(
    301         f"In the future `np.{attr}` will be defined as the "
    302         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    304 if attr in __former_attrs__:
--> 305     raise AttributeError(__former_attrs__[attr])
    307 # Importing Tester requires importing all of UnitTest which is not a
    308 # cheap import Since it is mainly used in test suits, we lazy import it
    309 # here to save on the order of 10 ms of import time for most users
    310 #
    311 # The previous way Tester was imported also had a side effect of adding
    312 # the full `numpy.testing` namespace
    313 if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

尝试降级NumPy时,又遇到编译错误:

error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for numpy
Failed to build numpy
ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects

需求:在不降级NumPy、不消除警告的前提下解决该问题。

解决方案

以下方法无需降级NumPy,也不会屏蔽警告,直接修复兼容性问题:

  • 方法1:升级Pandas到兼容版本
    Pandas 2.2.2对NumPy 1.26.x的兼容性存在瑕疵,升级到Pandas 2.2.3及以上版本即可解决np.bool的引用问题。执行命令:

    pip install --upgrade pandas
    
  • 方法2:临时映射np.bool到np.bool_
    如果暂时无法升级Pandas,可以在导入Pandas之前,手动给NumPy添加属性映射,让Pandas调用np.bool时自动指向正确的np.bool_:

    import numpy as np
    # 手动补全Pandas需要的属性映射
    np.bool = np.bool_
    # 之后正常导入所需库
    import pandas as pd
    import seaborn as sns
    import sqlalchemy
    import plotly
    

    该方法仅临时补全缺失的属性引用,不会屏蔽任何警告,完全符合需求。

内容的提问来源于stack exchange,提问作者Arnav Garg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:55:16