I am working with the Polars library in Python and encountering type support errors while trying to implement a series operation function.
minimal reproducible example
from polars import Series, Float64
def range_norm(serie: Series) -> Series:
"""Normalize the Series by its range."""
min_val = serie.min()
range_val = serie.max() - min_val
return (serie - min_val) / range_val
When running this function, Pylance in strict mode generates the following errors:
Operator "-" not supported for types "PythonLiteral | None" and "PythonLiteral | None"
Operator "-" not supported for types "int" and "date"
Operator "-" not supported for types "int" and "time"
Operator "-" not supported for types "int" and "datetime"
Operator "-" not supported for types "int" and "timedelta"
Operator "-" not supported for types "int" and "str"
Operator "-" not supported for types "int" and "bytes"
Operator "-" not supported for types "int" and "List[Any]"
Operator "-" not supported for types "int" and "None"
Operator "-" not supported for types "int" and "None"
[...]
I attempted to explicitly cast the min and max values to Float64 to ensure the correct type operation, but the errors persist.
pl.Float64(serie.max()) - pl.Float64(serie.min())
Question
How can I resolve these type support issues to ensure that my function works with polars data types in Polars? What are the best practices for handling these types of errors and ensuring type safety, especially in strict mode with tools like Pylance?