ts.plot()和dataFrame.plot()抛出错误:"NameError: name '_converter'未定义"

8
当运行数据框或系列的plot()方法时,Python会抛出错误。错误的最后一行是NameError:name'_converter'未定义
我正在使用Python 3.6,所有其他功能都按预期工作,因此不确定可能导致此问题的原因。
以下是导致该问题的代码示例,下面是结果的错误。
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()

返回的错误如下所示:
NameError                                 Traceback (most recent call last)
<ipython-input-336-8fe4bd433d4d> in <module>()
----> 1 ts.plot()
      2 
      3 plt.plot(ts)

c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   2501                            colormap=colormap, table=table, yerr=yerr,
   2502                            xerr=xerr, label=label, secondary_y=secondary_y,
-> 2503                            **kwds)
   2504     __call__.__doc__ = plot_series.__doc__
   2505 

c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   1925                  yerr=yerr, xerr=xerr,
   1926                  label=label, secondary_y=secondary_y,
-> 1927                  **kwds)
   1928 
   1929 

c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   1725                             pass
   1726                 data = series
-> 1727         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1728 
   1729     plot_obj.generate()

c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in __init__(self, data, **kwargs)
    929 
    930     def __init__(self, data, **kwargs):
--> 931         MPLPlot.__init__(self, data, **kwargs)
    932         if self.stacked:
    933             self.data = self.data.fillna(value=0)

c:\users\fguih\appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_core.py in __init__(self, data, kind, by, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, fig, title, xlim, ylim, xticks, yticks, sort_columns, fontsize, secondary_y, colormap, table, layout, **kwds)
     98                  table=False, layout=None, **kwds):
     99 
--> 100         _converter._WARN = False
    101         self.data = data
    102         self.by = by

NameError: name '_converter' is not defined

这似乎是您的pandas版本中的一个错误。使用pandas 0.20.1,代码按预期工作。 - ImportanceOfBeingErnest
我遇到了相同的错误。可能不是你要找的解决方案,但我将我的Anaconda环境降级到Python 3.5,然后它可以正常工作了。 - blueether
1
如果您确实使用的是最新版本的pandas,您可以在pandas问题跟踪器上报告此问题。这可能是代码中的错误(似乎代码实际上假定它可能会失败,因为from pandas.plotting import _converter被放置在try/except子句中,但不清楚原因),或者是文档缺少更新。 - ImportanceOfBeingErnest
3个回答

7

根据nbkhope的评论:

重新启动你的python解释器

在我的情况下,停止并重新启动jupyter notebook

(我花了太长时间来调试这个问题,所以我把它发布为答案)


3

如果您在绘图之前使用 pd.Series.asfreq 将时间序列捕捉到每日频率,那么您就可以使其正常工作:

ts.asfreq('D').plot(); 
plt.show()

enter image description here


谢谢您的回复。不过,如果可能的话,能否告知问题所在呢?因为代码和示例都来自于Pandas官网,所以我认为应该按预期工作。链接如下:http://pandas.pydata.org/pandas-docs/stable/10min.html#plotting - wilson_smyth
@wilson_smyth 首先需要知道您正在使用哪个版本的pandas。 - ImportanceOfBeingErnest
pd.version 返回 0.22.0。 - wilson_smyth
从http://pandas.pydata.org/pandas-docs/stable/10min.html#plotting的第一个示例开始,会导致出现错误。请改进答案并解释您为什么要这样做才能使其正常工作。 - nbkhope
1
对于那些遇到相同问题的人(https://github.com/pandas-dev/pandas/issues/19340),请尝试重新启动您的Python解释器。 - nbkhope
@nbkhope 我明白你的观点,但这个答案更像是一种“嗯,我不知道问题出在哪里,但这对我有效”的回答。我知道这有点糟糕,因为它并没有完全回答问题,我也接受了那个负评。如果你能帮我解释一下为什么这个方法有效,我会非常感激。 - cs95

0

使用Pandas 0.22.*遇到了问题
升级到Pandas 0.23.0版本。
问题似乎得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接