2013-03-18 46 views
3

爲什麼沒有開發商允許在.IX位操作?好奇這究竟是技術約束還是我忽略的邏輯問題。位運算上.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' 

''」

+2

嘗試用圓括號圍繞您的參數。 'df.ix [(df [「ptdelta」] <= 0)&(df [「ptdelta」]> 5)]'。 – bdiamante 2013-03-18 16:12:54

回答

6

我想你誤會這是怎麼回事廣場brackets-- .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 

否則,你要嘗試執行的(大概)一個float按位運算。例如,如果它是一個整數,那麼它會「工作」:

>>> 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