编辑
终于找到了这个bug-
Issue 9369
如问题所示-
左=0>s工作(例如python标量)。所以我认为这是
被视为一个0维数组(它是一个np.int64)(当
调用。)我会标记为bug。请随意挖掘
将比较运算符与
numpy
比较运算符左侧的数据类型(如np.int64或np.float64等)。一个简单的修复方法可能如@santon在回答中所指出的那样,将数字转换为python标量,而不是使用
numpy的复数
标量。
旧版本:
我试过潘达斯0.16.2。
我对你的原始df做了以下操作-
In [22]: df['z'] = df['x'].mean() < df['x']
In [23]: df
Out[23]:
x y z
0 1 1 False
1 2 1 False
2 3 1 True
3 4 9 True
In [27]: df['z'].mean() < df['z']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-afc8a7b869b4> in <module>()
----> 1 df['z'].mean() < df['z']
C:\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
586 return NotImplemented
587 elif isinstance(other, (np.ndarray, pd.Index)):
--> 588 if len(self) != len(other):
589 raise ValueError('Lengths must match to compare')
590 return self._constructor(na_op(self.values, np.asarray(other)),
TypeError: len() of unsized object
对我来说,这似乎是一个bug,我可以将布尔means与int进行比较,反之亦然,但唯一的问题是将布尔mean与布尔值进行比较(尽管我认为将mean()用于布尔值是不合理的)-
In [24]: df['z'] < df['x']
Out[24]:
0 True
1 True
2 True
3 True
dtype: bool
In [25]: df['z'] < df['x'].mean()
Out[25]:
0 True
1 True
2 True
3 True
Name: z, dtype: bool
In [26]: df['x'].mean() < df['z']
Out[26]:
0 False
1 False
2 False
3 False
Name: z, dtype: bool
我尝试在Pandas 0.16.1中复制该问题,也可以使用-
In [10]: df['x'].mean() < df['x']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-4e5dab1545af> in <module>()
----> 1 df['x'].mean() < df['x']
/opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/pandas/core/ops.pyc in wrapper(self, other, axis)
586 return NotImplemented
587 elif isinstance(other, (np.ndarray, pd.Index)):
--> 588 if len(self) != len(other):
589 raise ValueError('Lengths must match to compare')
590 return self._constructor(na_op(self.values, np.asarray(other)),
TypeError: len() of unsized object
In [11]: df['x'] < df['x'].mean()
Out[11]:
0 True
1 True
2 False
3 False
Name: x, dtype: bool
这似乎也是Pandas版本0.16.2中修复的一个bug(布尔值与整数混合时除外)。我建议使用-
pip install pandas --upgrade