I have a dataset that looks like this, where the third column is derived by dividing the first column by the second:
A_CLOSE_PRICE B_CLOSE_PRICE A_CLOSE_PRICE/B_CLOSE_PRICE
0 113.55 0.00 inf
1 97.85 80.00 1.223125
2 60.00 70.00 0.857143
3 51.65 51.65 1.000000
4 53.50 NaN NaN
5 NaN 1649.60 NaN
6 40.00 40.50 0.987654
7 1.10 1.00 1.100000
As I want to display the rows containing more than a 10% difference, I run this:
(df['A_CLOSE_PRICE/B_CLOSE_PRICE'] - 1 ).abs() > 0.1
But the last row as shown below returns me "True" instead of "False", which looks to me like a floating point issue. Does anyone know what should be the proper handling for this so I can get the correct results?
0 True
1 True
2 True
3 False
4 False
5 False
6 False
7 True
Yes you have a floating point issue, I think you can use the df.pct_change builtin directly on axis=1 with np.isclose to handle floating poinit comparison
s = df[['B_CLOSE_PRICE','A_CLOSE_PRICE']].pct_change(axis=1).iloc[:,-1].abs()
s.gt(0.1) & ~np.isclose(s-0.1,0)
0 True
1 True
2 True
3 False
4 False
5 False
6 False
7 False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With