My dataframe looks like this:
timestamp topAsk topBid CPA midprice CPB spread s
0 2019-03-14 00:00:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
1 2019-03-14 00:01:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
2 2019-03-14 00:02:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
3 2019-03-14 00:03:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
4 2019-03-14 00:04:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
5 2019-03-14 00:05:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
6 2019-03-14 00:06:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
7 2019-03-14 00:07:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
8 2019-03-14 00:08:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
9 2019-03-14 00:09:00 0.00005000 0.00004957 0.00004979 0.00004979 0.00004979 4.3E-7 0.008636272343844145410725045190
When I try to add a new column using the following line of code: df['gamma'] = ((df['midprice'] - df['CPB']) / df['spread']) I have the following error message= Pandas error: [<class 'decimal.DivisionUndefined'>]
Is it because my column df['spread'] is really small? I am a bit stuck, thanks!
df.info() shows:

decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>] means that you have somewhere a 0/0 division using Decimal values. It is easy to find a workaround by first testing whether df['spread'] is 0, but you really should try to find why and how a value that is intended to be a divisor can be null. And in that case I would use NaN as result. Code could be:
df['gamma'] = df.apply(lambda x:
(x['midprice'] - x['CPB']) / x['spread'] if x['spread'] != 0
else decimal.Decimal('NaN'), axis=1)
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