【问题标题】:ts.plot() and dataFrame.plot() throwing error: " NameError: name '_converter' is not defined"ts.plot() 和 dataFrame.plot() 抛出错误:“NameError: name '_converter' is not defined”
【发布时间】:2018-06-28 16:51:36
【问题描述】:

当运行数据框或系列的plot() 方法时,python 抛出错误。错误的最后一行是NameError: name '_converter' is not defined

我使用的是 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 0.20.1,代码按预期工作。
  • 我遇到了同样的错误。可能不是您正在寻找的解决方案,但我将 Anaconda 环境降级到 Python 3.5 并且运行良好。
  • 如果您确实在使用最新的 pandas 版本,您可以在the pandas issue tracker 上报告此问题。然后它要么是代码中的错误(似乎代码实际上假设它可能会失败,因为from pandas.plotting import _converter 被放入try/except clause,但不清楚原因。)或者它是缺少文档更新。

标签: python pandas matplotlib time-series


【解决方案1】:

基于 nbkhope 的评论:

重启你的python解释器

就我而言,停止并重新开始jupyter notebook

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

【讨论】:

    【解决方案2】:

    如果您在绘图前使用pd.Series.asfreq 将时间序列捕捉到每日频率,则可以实现此目的:

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

    【讨论】:

    【解决方案3】:

    在使用 pandas 0.22 时遇到问题。*
    更新到 pandas 0.23.0。
    问题似乎已解决。

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2022-11-27
      • 1970-01-01
      • 2015-11-11
      相关资源
      最近更新 更多