Python pylab errorbar和pandas的问题

Python pylab errorbar和pandas的问题,python,matplotlib,pandas,Python,Matplotlib,Pandas,当我尝试同时使用pandas和pylab.errorbar时,会收到一条非常混乱的错误消息 我有一个名为summary的数据框: year treatment SE mean test 0 2012 Control 0.452069 0.904387 A 1 2012 Pesticide 0.246149 1.365210 B 2 2013 Control 1.073697 3.649746 A 3 2013

当我尝试同时使用pandas和pylab.errorbar时,会收到一条非常混乱的错误消息

我有一个名为summary的数据框:

   year  treatment        SE      mean test
0  2012    Control  0.452069  0.904387    A
1  2012  Pesticide  0.246149  1.365210    B
2  2013    Control  1.073697  3.649746    A
3  2013  Pesticide  1.234574  2.593122    B
当我尝试绘制errorbar图时:

for n in summary['treatment'].unique():
    py.errorbar(summary[summary['treatment'] == n]['year'], 
                summary[summary['treatment'] == n]['mean'], 
                summary[summary['treatment'] == n]['SE'])
我得到这个错误:

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-171-12bf2023b562>", line 2, in <module>
    py.errorbar(summary[summary['treatment'] == n]['year'], summary[summary['treatment'] == n]['mean'], summary[summary['treatment'] == n]['SE'])
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2697, in errorbar
    errorevery=errorevery, capthick=capthick, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 5749, in errorbar
    iterable(yerr[0]) and iterable(yerr[1])):
  File "/Library/Python/2.7/site-packages/pandas/core/series.py", line 618, in __getitem__
    return self.index.get_value(self, key)
  File "/Library/Python/2.7/site-packages/pandas/core/index.py", line 724, in get_value
    return self._engine.get_value(series, key)
  File "index.pyx", line 96, in pandas.index.IndexEngine.get_value (pandas/index.c:2873)
  File "index.pyx", line 104, in pandas.index.IndexEngine.get_value (pandas/index.c:2685)
  File "index.pyx", line 148, in pandas.index.IndexEngine.get_loc (pandas/index.c:3422)
  File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6570)
  File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6511)
KeyError: 0
这是:

py.errorbar(summary[summary['treatment'] == 'Pesticide']['year'],
            summary[summary['treatment'] == 'Pesticide']['mean'],
            yerr = summary[summary['treatment'] == 'Control']['SE'], fmt = 'o')
但不是这个:

py.errorbar(summary[summary['treatment'] == 'Pesticide']['year'],
            summary[summary['treatment'] == 'Pesticide']['mean'],
            yerr = summary[summary['treatment'] == 'Pesticide']['SE'], fmt = 'o')
因此,在耶尔的“杀虫剂”处理是造成问题的原因。我在过去成功地使用了类似的循环,因此有一些特定于此设置。注意:我还使用了“test”列代替了“treatment”列,得到了相同的错误


思考一下为什么会失败?

错误看起来与mpl无关。
summary[summary['treatment']=='mediator']['SE']
工作正常吗?\n我还发现,如果我将yerr转换为numpy数组,如np.array(summary…['SE']),它工作正常。我只是不明白为什么我需要在这里这样做,而我以前没有。您是否升级到最新的
pandas
?他们改变了
系列
而不是最近从Ndarray派生出来的,Ndarray破坏了mpl中的各种东西。我不这么认为。我用的是熊猫0.12.0。这段代码可以很好地处理另一组数据。这一套有点特别。
py.errorbar(summary[summary['treatment'] == 'Pesticide']['year'],
            summary[summary['treatment'] == 'Pesticide']['mean'],
            yerr = summary[summary['treatment'] == 'Pesticide']['SE'], fmt = 'o')