2

这对我来说看起来像是一个错误pandas.Series.alldf是一个 PandasDataFrame对象,pd是 的简写pandas):

In [18]: df.foo.apply(lambda x: x.startswith(u'bar').head()
Out[18]:
0    True
1    False
2    True
3    True
4    False
Name: foo

In [19]: (df.baz == u'frobozz').head()
Out[19]: 
0    False
1    False
2    True
3    True
4    False
Name: baz

In [20]: (type(Out[20]), type(Out[19]))
Out[20]: (pandas.core.series.Series, pandas.core.series.Series)

In [21]: pd.Series.all(Out[18], Out[19])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-310-d132f431d45f> in <module>()
----> 1 pd.Series.all(Out[18], Out[19])

/home/jones/.virtualenvs/proj/local/lib/python2.7/site-packages/pandas/core/series.pyc in f(self, *args, **kwargs)
    276     @Appender(func.__doc__)
    277     def f(self, *args, **kwargs):
--> 278         result = func(self, *args, **kwargs)
    279         if isinstance(result, np.ndarray) and result.ndim == 0:
    280             # return NumPy type

/home/jones/.virtualenvs/proj/local/lib/python2.7/site-packages/numpy/core/_methods.pyc in _all(a, axis, dtype, out, keepdims)
     28 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
     29     return um.logical_and.reduce(a, axis=axis, dtype=dtype, out=out,
---> 30                                  keepdims=keepdims)                                                                                                                       
     31
     32 def _count_reduce_items(arr, axis):

TypeError: only length-1 arrays can be converted to Python scalars

这是怎么回事?

4

2 回答 2

1

从文档中, pd.Series.all 似乎只采用一个 Series 对象。尝试这个 -

pd.Series.all(Out[18].append(Out[19]))
于 2013-03-25T23:15:42.700 回答
1

这对我来说似乎不是一个错误,但我不知道你的想法pd.Series.all(Out[18], Out[19])

>>> pd.Series.all?
Type:       instancemethod
String Form:<unbound method Series.all>
File:       /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.10.1-py2.7-macosx-10.6-intel.egg/pandas/core/series.py
Definition: pd.Series.all(self, *args, **kwargs)
Docstring:
a.all(axis=None, out=None)

Returns True if all elements evaluate to True.

您正在使用类中的版本,因此第一个参数被解释为实例,第二个参数被解释为轴。 pandas正在尝试将Series您传递的第二个整数转换为一个整数以将其理解为一个轴,如果数组的长度> 1,这将无法工作。

于 2013-03-25T23:17:08.387 回答