What is the best way to avoid divide by zero with Explorer.Series.divide/2

I have two Explorer.Series and want to divide the first series with the second series containing 0.

series0 = Explorer.Series.from_list([1, 2, 3])
series1 = Explorer.Series.from_list([0, 1, 2])

Explorer.Series.divide(series0, series1)

Then it throws an error,

thread '<unnamed>' panicked at 'attempt to divide by zero', /rustc/dc80ca78b6ec2b6bba02560470347433bcd0bb3c/library/core/src/ops/arith.rs:507:1
 ** (ErlangError) Erlang error: :nif_panicked
    (explorer 0.2.0) Explorer.PolarsBackend.Native.s_div(shape: (3,)
Series: '' [i64]
[
        1
        2
        3
], shape: (3,)
Series: '' [i64]
[
        0
        1
        2
])
    (explorer 0.2.0) lib/explorer/polars_backend/shared.ex:17: Explorer.PolarsBackend.Shared.apply_series/3

I think NaN or Infinity is the right result for dividing by zero. But I don’t know how to get that result with Explorer.Series.divide/2.

I got the answer.

1 Like