-1

I am using polars sql_expr() for evaluating the dataframe and while evaluating the result found that sql_expr is giving incorrect output for a simple calculation like "2+4/5".

for example:

polars dataframe:

col1 col2 col3
jan-23 2 5
feb-23 2 4

expected result:

col1 col2 col3 result
jan-23 2 5 3.0000
feb-23 2 4 2.8000

output obtained:

col1 col2 col3 result
jan-23 2 5 3.0000
feb-23 2 4 2.0000
df = pl.DataFrame("col1":["jan-23", "feb-23"],"col2":[2,2], "col3:[5,4])
formula_rhs = "col2 + col3 / 5"
col_name ="result"
df.with_columns(pl.sql_expr(formula_rhs).alias(col_name).cast(pl.Float64)) 

also changed pl.Float64 to pl.Decimal.

  • Change the `5` into a floating point number (`5.0`) and see what that does. Looks like integer division is happening, and casting the result into float after the calculation already occurred won't really help you get the decimal places back. – Dan Mašek Aug 07 '23 at 09:33
  • 1
    @DanMašek by changing 5 to 5.0 doesn't helped its still providing the same output. – dhruv bothra Aug 07 '23 at 09:37
  • 2
    If I fix the syntax errors and change to `formula_rhs = "col2 + col3 / 5.0"` I get the expected 2.8. – Ture Pålsson Aug 07 '23 at 10:16

0 Answers0