polars icon indicating copy to clipboard operation
polars copied to clipboard

Inconsistent `1 // 0` behavior between Expr/Series and DataFrame

Open Wainberg opened this issue 1 year ago • 1 comments

Checks

  • [X] I have checked that this issue has not already been reported.
  • [X] I have confirmed this bug exists on the latest version of Polars.

Reproducible example

Expr and Series: 1 // 0 = 0, with no error:

>>> pl.DataFrame({'a': [1], 'b': [0]}).select(pl.col.a // pl.col.b)
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 0   │
└─────┘
>>> pl.Series([1]) // pl.Series([0])
shape: (1,)
Series: '' [i64]
[
        0
]

DataFrame: 1 // 0 gives an error:

>>> pl.DataFrame({'a': [1]}) // pl.DataFrame({'a': [0]})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/polars/py-polars/polars/dataframe/frame.py", line 1496, in __floordiv__
    return self._div(other, floordiv=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/polars/py-polars/polars/dataframe/frame.py", line 1486, in _div
    return df.with_columns(int_casts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/polars/py-polars/polars/dataframe/frame.py", line 8270, in with_columns
    return self.lazy().with_columns(*exprs, **named_exprs).collect(_eager=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/polars/py-polars/polars/lazyframe/frame.py", line 1730, in collect
    return wrap_df(ldf.collect())
                   ^^^^^^^^^^^^^
polars.exceptions.ComputeError: conversion from `f64` to `i64` failed in column 'a' for 1 out of 1 values: [inf]

Log output

No response

Issue description

Expr/Series have different integer division by zero behavior from DataFrame.

Expected behavior

This should always give an error, or at least a warning.

Installed versions

--------Version info---------
Polars:               0.20.4
Index type:           UInt32
Platform:             Linux-4.4.0-22621-Microsoft-x86_64-with-glibc2.35
Python:               3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 08:03:24) [GCC 12.3.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          3.0.0
connectorx:           <not installed>
deltalake:            0.14.0
fsspec:               2023.12.2
gevent:               <not installed>
hvplot:               0.9.1
matplotlib:           3.8.2
numpy:                1.26.2
openpyxl:             <not installed>
pandas:               2.1.4
pyarrow:              14.0.2
pydantic:             2.5.3
pyiceberg:            0.5.1
pyxlsb:               <not installed>
sqlalchemy:           2.0.23
xlsx2csv:             <not installed>
xlsxwriter:           3.1.9

Wainberg avatar Jan 13 '24 20:01 Wainberg

Relevant: https://github.com/pola-rs/polars/issues/9620

Wainberg avatar Jan 13 '24 20:01 Wainberg

I'll take care of this tomorrow, ensuring we give a proper error.

orlp avatar Jan 23 '24 16:01 orlp