【发布时间】:2013-03-15 14:50:27
【问题描述】:
在我看来,这就像 pandas.Series.all 中的一个错误(df 是 Pandas DataFrame 对象,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
发生了什么事?
【问题讨论】:
-
+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