熊猫错误:[ ]

问题描述 投票:0回答:1

我的数据框看起来像这样:

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

当我尝试使用以下代码行添加新列时:df['gamma'] = ((df['midprice'] - df['CPB']) / df['spread'])我有以下错误消息= Pandas error: [<class 'decimal.DivisionUndefined'>]

是因为我的专栏df['spread']真的很小吗?我有点卡住了,谢谢!

df.info()显示:

enter image description here

python pandas
1个回答
0
投票

decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]意味着你有一个使用0/0值的Decimal分区。通过首先测试df['spread']是否为0很容易找到变通方法,但是你真的应该试着找出为什么以及如何将一个除数的值设为null。在那种情况下,我会使用NaN作为结果。代码可以是:

df['gamma'] = df.apply(lambda x:
     (x['midprice'] - x['CPB']) / x['spread'] if x['spread'] != 0
     else decimal.Decimal('NaN'), axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.