.ix上的按位运算(Pandas)

Mic*_*lly 3 dataframe python-2.7 pandas

为什么开发人员不允许在.ix上进行按位操作?好奇这是技术约束还是我忽视的逻辑问题.

df.ix[df["ptdelta"]<=0 & df["ptdelta"]>5]
Run Code Online (Sandbox Code Playgroud)

追溯是:

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'
Run Code Online (Sandbox Code Playgroud)

"""

:由于熊猫v0.20中,.ix 索引器已被弃用赞成.iloc/ .loc.

DSM*_*DSM 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
Run Code Online (Sandbox Code Playgroud)

否则,你试图执行按位操作(可能是)浮点数.例如,如果它是一个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
Run Code Online (Sandbox Code Playgroud)