error in expression evaluation with float() conversion

Viewed 40

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.

2 Answers

An important thing to realize about chained comparisons is that (a > b) >= c is not the same as a > b >= c. For example:

>>> 0 > 1 >= 0
False
>>> (0 > 1) >= 0
True

The reason your old code and new code are returning different results is because the old code was incorrect.

thanks for your answer, i've relized thay my old code was wrong, but the real question is why the behviour changes with a float conversion.

    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
Related