【问题标题】:Type Error in PyCharm Using "Pandas" and "Seaborn"使用“Pandas”和“Seaborn”在 PyCharm 中输入错误
【发布时间】:2022-01-05 10:53:00
【问题描述】:

我目前正在使用带有 IDE PyCharm 的 Python 来绘制在带有 seaborn 的 pandas 中生成的数据框。我正在应用以下代码:

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

netflix_stocks = pd.read_csv("NFLX.csv")
dowjones_stocks = pd.read_csv("DJI.csv")
netflix_stocks_quarterly = pd.read_csv("NFLX_daily_by_quarter.csv")

# rename columns
netflix_stocks.rename(columns = {'Adj Close': 'Price'}, inplace = True)
dowjones_stocks.rename(columns = {'Adj Close': 'Price'}, inplace = True)
netflix_stocks_quarterly.rename(columns = {'Adj Close': 'Price'}, inplace = True)


# visualizing the netflix quarterly data
ax = plt.subplot()
sns.violinplot(data = netflix_stocks_quarterly, x = "Quarter", y = "Price")
ax.set_title("Distribution of 2017 Netflix Stock Prices by Quarter")
plt.xlabel("Business Quarters in 2017")
plt.ylabel("Closing Stock Price")

plt.savefig("Distribution of 2017 Netflix Stock Prices by Quarter.png")
plt.show()

这导致我看到错误消息

Traceback (most recent call last):
  File "D:\testproject\venv\lib\site-packages\numpy\core\getlimits.py", line 459, 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 "D:\Program Files (x86)\PyCharm Community Edition 2021.3.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\Program Files (x86)\PyCharm Community Edition 2021.3.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/testproject/main.py", line 18, in <module>
    sns.violinplot(data = netflix_stocks_quarterly, x = "Quarter", y = "Price")
  File "D:\testproject\venv\lib\site-packages\seaborn\_decorators.py", line 46, in inner_f
    return f(**kwargs)
  File "D:\testproject\venv\lib\site-packages\seaborn\categorical.py", line 2408, in violinplot
    plotter.plot(ax)
  File "D:\testproject\venv\lib\site-packages\seaborn\categorical.py", line 1043, in plot
    self.draw_violins(ax)
  File "D:\testproject\venv\lib\site-packages\seaborn\categorical.py", line 801, in draw_violins
    self.draw_box_lines(ax, violin_data, support, density, i)
  File "D:\testproject\venv\lib\site-packages\seaborn\categorical.py", line 967, in draw_box_lines
    ax.scatter(center, q50,
  File "D:\testproject\venv\lib\site-packages\matplotlib\__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "D:\testproject\venv\lib\site-packages\matplotlib\axes\_axes.py", line 4487, in scatter
    self.add_collection(collection)
  File "D:\testproject\venv\lib\site-packages\matplotlib\axes\_base.py", line 2242, in add_collection
    self._unstale_viewLim()
  File "D:\testproject\venv\lib\site-packages\matplotlib\axes\_base.py", line 776, in _unstale_viewLim
    self.autoscale_view(**{f"scale{name}": scale
  File "D:\testproject\venv\lib\site-packages\matplotlib\axes\_base.py", line 2932, in autoscale_view
    handle_single_axis(
  File "D:\testproject\venv\lib\site-packages\matplotlib\axes\_base.py", line 2895, in handle_single_axis
    x0, x1 = locator.nonsingular(x0, x1)
  File "D:\testproject\venv\lib\site-packages\matplotlib\ticker.py", line 1654, in nonsingular
    return mtransforms.nonsingular(v0, v1, expander=.05)
  File "D:\testproject\venv\lib\site-packages\matplotlib\transforms.py", line 2880, in nonsingular
    if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
  File "D:\testproject\venv\lib\site-packages\numpy\core\getlimits.py", line 462, in __new__
    dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable
python-BaseException
Backend TkAgg is interactive backend. Turning interactive mode on.

由于某种原因,相同的代码可以在 jupyter notebook 下运行。我正在导入以下版本的 pandas、matplotlib 和 seaborn:

versions of pckages imported

另外,我使用虚拟环境创建了一个项目,如下:

project creation with a virtual environment

由于数据帧没有“空”条目或必须更改数据类型的条目,我无法找出原因。应绘制的数据框如下所示:

dataframe

感谢任何建议!

提前致谢, 乔纳斯

【问题讨论】:

  • Jupyter notebook 和 IDE 的 python 环境是否相同?还请发布指向实际数据的链接而不是图片,以便人们可以重新创建问题
  • 嗨! *.csv 文件可以在github.com/jonasbauer192/… 找到。此外,我使用带有 miniconda 的 jupyter notebook 和带有 VirtualEnv 的 PyCharm。但我也尝试在 PyCharm 中使用 Conda 环境,这确实会导致同样的错误。
  • 我只使用了 Python 3.9.8 而不是 3.10.1,并且由于某种原因它没有问题。

标签: python pandas pycharm seaborn typeerror


【解决方案1】:

我遇到了类似的问题,我通过降级 numpy 包解决了它。 我使用的是 1.22.3 并更改为 1.22.2。

pip3 卸载 numpy

然后

pip3 卸载 numpy==1.22.2

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    相关资源
    最近更新 更多