Skip to content

[Enh](discussion): float // 0.0 returns null on every backend except Polars, which one is intended? #4070

Open

Description

@ReguiguiMohamed

Follow-up from #3994, as @FBruzzesi suggested there. Related to #2748.

Floor-dividing a float column by 0.0 gives inf, -inf and NaN on Polars, and null on every other backend.

Code Example:

import narwhals as nw
import pandas as pd
import polars as pl

data = {"a": [1.0, -1.0, 0.0], "b": [0.0, 0.0, 0.0]}
for native in (pl.DataFrame(data), pd.DataFrame(data)):
    result = nw.from_native(native).select(r=nw.col("a") // nw.col("b"))["r"]
    print(result.to_list(), result.null_count())

Result yielded:

[inf, -inf, nan] 0
[nan, nan, nan] 3

Same check on the other backends:

backend [1.0, -1.0, 0.0] // 0.0
polars [inf, -inf, nan]
pandas, pandas[nullable], pandas[pyarrow] null
pyarrow, dask null
duckdb, sqlframe, ibis null

The null mostly comes from narwhals here. pandas and pyarrow return inf, -inf and NaN natively, and narwhals masks them in floordiv_null_on_zero and floordiv_compat. The pyarrow one says the mask is there to match Polars, but Polars only returns null for integer division by zero.
DuckDB here is the exception, it returns null on its own.

No test ensures this yet, division_by_zero_test.py only uses // on the int column.

Which would be better? I'd say to follow Polars, which is what pandas and pyarrow do natively, and which narwhals does for / on floats already. But if keeping null everywhere is easier for the SQL backends, I think that we should document that as a known difference from Polars.

Versions: narwhals main (455269e), polars 1.44.2, pandas 3.0.6, pyarrow 25.0.1, duckdb 1.5.5, dask 2026.8.0, ibis 12.0.0.

Activity

  1. changed the title [-][Enh](discussion): float '// 0.0' returns null on every backend except Polars, which one is intended?[/-] [+][Enh](discussion): float `// 0.0` returns null on every backend except Polars, which one is intended?[/+] on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions