3

为什么开发人员不允许对 .ix 进行按位运算?好奇这是技术限制问题还是我忽略的逻辑问题。

df.ix[df["ptdelta"]<=0 & df["ptdelta"]>5]

回溯是:

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'

'''

注意:从 Pandas v0.20 开始,.ix 不推荐使用索引器以支持.iloc/ .loc

4

1 回答 1

7

我认为您误解了方括号内.ix发生的事情 - 与它无关。如果你适当地括号,这有效:

>>> df = pd.DataFrame({"a": [-1,2,-3,4,6,-8.2]})
>>> df.ix[(df['a'] <= 0) | (df['a'] > 5)]
     a
0 -1.0
2 -3.0
4  6.0
5 -8.2

否则,您将尝试对(大概)浮点数执行按位运算。例如,如果它是一个 int,那么它会“工作”:

>>> df['a'] <= 0 & df['a']
Traceback (most recent call last):
  File "<ipython-input-40-9173361ec31b>", line 1, in <module>
    df['a'] <= 0 & df['a']
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'

>>> df['a'] <= 0 & df['a'].astype(int)
0     True
1    False
2     True
3    False
4    False
5     True
Name: a, Dtype: bool
于 2013-03-18T16:13:21.407 回答