Bitwise operations on .ix (Pandas)
dataframe, pandas, python-2.7
Solution
I think you're misunderstanding what's going on inside the square brackets-- `.ix` has nothing to do with it. If you parenthesize appropriately, this works:
>>> 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
Otherwise you're trying to perform a bitwise op on (presumably) a float. If it were an int, for example, then it'd "work":
>>> 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
Problem
Why didn't the developers allow bitwise operations on .ix? Curious whether it is a matter of technical constraint or a logical issue I'm overlooking. ``` df.ix[df["ptdelta"]<=0 & df["ptdelta"]>5] ``` The traceback is: ``` 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' ``` ''' Note: As of Pandas v0.20, `.ix` indexer is deprecated in favour of `.iloc` / `.loc`.