i have a pandas dataframe and an old routine that was making evaluations iterating trough lines, i wanted to speed it up and i've started using filters on pandas columns instead of iterating and i've came a cross an odd behaviour:
this is the original code (inside a for loop that iterates trough all the lines of the dataframe passed to the function).
if (float(_data['MACD'][index_value] - _data['MACD SIGNAL'][index_value]) > float(0)) >= float(_data['MACD'][index_value - 1] - _data['MACD SIGNAL'][index_value - 1]):
gauge_value += 1
This is my new code
_data['MACD_SIGNAL_DIFF']=_data['MACD']-_data['MACD SIGNAL']
_data.loc[(_data['MACD_SIGNAL_DIFF']>0.0) & (_data['MACD_SIGNAL_DIFF+1']<=0.0,'Gauge']+=1
I've noticed that the 2 routines are giving different values for the gauge value, i've did some research and it turned out that the float conversion is messin all up, here is my findings:
index_value=10
(float(df['MACD'][index_value] - df['MACD SIGNAL'][index_value]) > float(0)) >= float(df['MACD'][index_value - 1] - df['MACD SIGNAL'][index_value - 1])
Out[32]: True
df['MACD'][index_value]-df['MACD SIGNAL'][index_value]
Out[35]: 1.4002172909618273e-06
df['MACD'][index_value-1]-df['MACD SIGNAL'][index_value-1]
Out[36]: 3.53445505941293e-07
1.4002172909618273e-06>0>3.53445505941293e-07
Out[37]: False
df['MACD'][index_value]-df['MACD SIGNAL'][index_value]>0>df['MACD'][index_value-1]-df['MACD SIGNAL'][index_value-1]
Out[38]: False
(float(df['MACD'][index_value] - df['MACD SIGNAL'][index_value]) > float(0)) >= float(df['MACD'][index_value - 1] - df['MACD SIGNAL'][index_value - 1])
Out[39]: True
As you can see if i force the conversion from numpy.float64 to float the evaluation of the expression turns out to be wrong as you can see from the above console lines.
Any help is apreciated as to me the old code is giving wrong values.