当我试图简单地从假数据绘制虚拟直方图(从pandas文档中复制/粘贴)时,我遇到了一个奇怪的问题。 导致这次崩溃的原因是什么?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'A' : [1,2,3,4],
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]),
'F' : 'foo' })
c=['A']
plt.figure()
plt.hist(df[c])
plt.show()
使用ipython2 ttplot.py -i
运行时,我获得以下输出:
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jul 2 2016, 17:42:40)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/srv/logs/tiaki/sentryo/exps/tools/ttplot.py in <module>()
28
29 plt.figure()
---> 30 plt.hist(df[c])
31 plt.show()
32
/home/inendi/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.pyc in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
2956 histtype=histtype, align=align, orientation=orientation,
2957 rwidth=rwidth, log=log, color=color, label=label,
-> 2958 stacked=stacked, data=data, **kwargs)
2959 finally:
2960 ax.hold(washold)
/home/inendi/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
1810 warnings.warn(msg % (label_namer, func.__name__),
1811 RuntimeWarning, stacklevel=2)
-> 1812 return func(ax, *args, **kwargs)
1813 pre_doc = inner.__doc__
1814 if pre_doc is None:
/home/inendi/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5993 xmax = -np.inf
5994 for xi in x:
-> 5995 if len(xi) > 0:
5996 xmin = min(xmin, xi.min())
5997 xmax = max(xmax, xi.max())
TypeError: len() of unsized object
In [1]:
使用ipython3 ttplot.py -i
,我获得以下输出:
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
2133 try:
-> 2134 return self._engine.get_loc(key)
2135 except KeyError:
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4164)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)()
KeyError: 0
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
/srv/logs/tiaki/sentryo/exps/tools/ttplot.py in <module>()
28
29 plt.figure()
---> 30 plt.hist(df[c])
31 plt.show()
32
/usr/lib/python3/dist-packages/matplotlib/pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
2894 histtype=histtype, align=align, orientation=orientation,
2895 rwidth=rwidth, log=log, color=color, label=label,
-> 2896 stacked=stacked, **kwargs)
2897 draw_if_interactive()
2898 finally:
/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5576 # Massage 'x' for processing.
5577 # NOTE: Be sure any changes here is also done below to 'weights'
-> 5578 if isinstance(x, np.ndarray) or not iterable(x[0]):
5579 # TODO: support masked arrays;
5580 x = np.asarray(x)
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in __getitem__(self, key)
2057 return self._getitem_multilevel(key)
2058 else:
-> 2059 return self._getitem_column(key)
2060
2061 def _getitem_column(self, key):
/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _getitem_column(self, key)
2064 # get column
2065 if self.columns.is_unique:
-> 2066 return self._get_item_cache(key)
2067
2068 # duplicate columns & possible reduce dimensionality
/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py in _get_item_cache(self, item)
1384 res = cache.get(item)
1385 if res is None:
-> 1386 values = self._data.get(item)
1387 res = self._box_item_values(item, values)
1388 cache[item] = res
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in get(self, item, fastpath)
3539
3540 if not isnull(item):
-> 3541 loc = self.items.get_loc(item)
3542 else:
3543 indexer = np.arange(len(self.items))[isnull(self.items)]
/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
2134 return self._engine.get_loc(key)
2135 except KeyError:
-> 2136 return self._engine.get_loc(self._maybe_cast_indexer(key))
2137
2138 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4164)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)()
KeyError: 0
In [1]:
不知何故,python2和3指向不同版本的matplotlib和pandas
Python2:matplotlib:'1.5.1',pandas:'0.18.1'
Python3:matplotlib:'1.4.2',pandas:'0.19.1'
答案 0 :(得分:1)
你应该使用df [c [0]],因为c是一个不是字符串的数组。