pythondataframecastingpython-polars

confused by silent truncation in polars type casting


I encountered some confusing behavior with polars type-casting (silently truncating floats to ints without raising an error, even when explicitly specifying strict=True), so I headed over to the documentation page on casting and now I'm even more confused.

The text at the top of the page says:

The function cast includes a parameter strict that determines how Polars behaves when it encounters a value that cannot be converted from the source data type to the target data type. The default behaviour is strict=True, which means that Polars will thrown an error to notify the user of the failed conversion while also providing details on the values that couldn't be cast.

However, the code example immediately below (section title "Basic example") shows a df with a floats column taking values including 5.8 being truncated to int 5 during casting with the code pl.col("floats").cast(pl.Int32).alias("floats_as_integers"), i.e. without strict=False.

What am I misunderstanding here? The text seems to indicate that this truncation, with strict=True as default, should "throw an error," but the code example in the documentation (and my own polars code) throws no error and silently truncates values.


Solution

  • It is accepted in Python (and more generally) that casting a float to an int will truncate the float and not raise an exception.

    E.g. in Python:

    >>> int(5.8)
    5
    

    Similarly, in Polars, casting a float to an int can be converted from the source data type to the target data type.

    For anyone else looking, this answer provides further detail / examples.