如何修复NumPy .dtype 'NoneType'错误

u2nhd7ah  于 2023-06-29  发布在  其他
关注(0)|答案(3)|浏览(173)

我在PyCharm调试模式下运行以下Python代码。

import numpy as np, pandas as pd, numpy.polynomial.chebyshev as chebyshev
from pathlib import Path

home = str(Path.home())

directory = '/Downloads'
d = pd.read_csv(home+directory+'/data.csv')

np.random.seed(0)
nData = 4
data = np.random.randn(nData,2)
z = data[:,0]
y = data[:,1]

coef = chebyshev.chebfit(z,y,3)

我遇到以下错误消息:

TypeError: 'NoneType' object is not callable

但是,如果我注解掉'd = ...'行,一切都正常。更奇怪的是,两个版本在运行模式下都运行良好。这是怎么回事?
错误的堆栈跟踪:

/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --cmd-line --multiproc --qt-support=auto --client 127.0.0.1 --port 65032 --file /Users/PycharmProjects/pythonProject/trial.py
warning: PYDEVD_USE_CYTHON environment variable is set to 'NO'. Frame evaluator will be also disabled because it requires Cython extensions to be enabled in order to operate correctly.
/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py:1844: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
  dummy_thread = threading.currentThread()
Connected to pydev debugger (build 212.5457.59)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/getlimits.py", line 384, in __new__
    dtype = numeric.dtype(dtype)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/PycharmProjects/pythonProject/trial.py", line 16, in <module>
    coef0 = chebyshev.chebfit(z0,y0,4)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/polynomial/chebyshev.py", line 1670, in chebfit
    return pu._fit(chebvander, x, y, deg, rcond, full, w)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/polynomial/polyutils.py", line 650, in _fit
    rcond = len(x)*np.finfo(x.dtype).eps
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/getlimits.py", line 387, in __new__
    dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable

Process finished with exit code 1
r6l8ljro

r6l8ljro1#

请升级到pandas >= 1.4.3修复此问题。你需要降级到Python 3.9,或者 * 升级到pandas 1.4.3* ~等到这个CPython/cython bug被修复~。

这个 was ~is~实际上是一个真实的的bug(截至2022-04-14),并且正在显示...:

  • 在cython中:#4609
  • 在CPython中:#90693(原始:bpo#46535
  • 在numpy:#21008
    **更新:**Pandas v1.4.3不存在此bug,升级Pandas将是另一种解决方案。
kuhbmx9i

kuhbmx9i2#

This answer很棒。但它应该被更新。

简短回答:

升级Pandas --> v1.4.3
你应该升级到pandas的v1.4.3。然后,代码现在的行为与调试器中的预期行为相同。

imzjd6km

imzjd6km3#

参见1
显然有一个PyCharmnumpy.core模块空模板替换了真正的numpy.core模块,它干扰了真正的模块。

相关问题