【问题标题】:"TypeError: only length-1 arrays can be converted to Python scalars"“TypeError:只有长度为 1 的数组可以转换为 Python 标量”
【发布时间】:2013-03-15 14:50:27
【问题描述】:

在我看来,这就像 pandas.Series.all 中的一个错误(df 是 Pandas DataFrame 对象,pdpandas 的简写):

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

发生了什么事?

【问题讨论】:

  • +1 我不知道你可以使用 Out[14] 等作为变量。
  • @AndyHayden:ipython 借鉴了 Mathematica 的一个想法(在我看来,它为任何类型的图形界面设定了标准,并且在过去 20 年左右一直在这样做) .顺便说一句,就像Out 可以被视为一个数组,In 也可以,尽管它的内容没有那么有用。 In 可能会派上用场的一种(当然是牵强的)情况是,例如,如果In[10] 是一个涉及x 的复杂表达式,我们简称它为E(x)x 的值有自从In[10] 被评估后发生了变化(产生现在Out[10] 中的内容)......
  • 然后可以运行eval(In[10]) 来获取E(x) 的更新值。 IOW,In 可以被认为是一个人的交互历史的“可寻址”形式,它允许一个人到达特定的输入表达式,而不必使用向上箭头对历史进行线性(反向)遍历.

标签: pandas


【解决方案1】:

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

pd.Series.all(Out[18].append(Out[19]))

【讨论】:

    【解决方案2】:

    这对我来说似乎不是一个错误,但我不知道你认为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,这将无法工作。

    【讨论】:

      猜你喜欢
      • 2014-09-27
      • 1970-01-01
      • 2016-08-06
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多