Skip to content

Commit 057ea6b

Browse files
committed
optimize polars typeconversion
1 parent 90d9b2a commit 057ea6b

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/odoo_data_flow/lib/preflight.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,22 +439,19 @@ def _auto_correct_field_types( # noqa: C901
439439
col_data = df.get_column(clean_field_name)
440440
# Check for float string values like "1.0", "2.0" in integer fields
441441
non_null_values = col_data.filter(col_data.is_not_null())
442-
for val in non_null_values:
443-
str_val = str(val)
444-
if str_val and str_val != "None" and str_val.strip() != "":
445-
# Check if it looks like a float string in an integer field
446-
if "." in str_val:
447-
try:
448-
float_val = float(str_val)
449-
if float_val.is_integer():
450-
# It's a whole number like "1.0" that should be
451-
# int
452-
corrections_needed = True
453-
break
454-
except ValueError:
455-
# Not a valid float, will be handled by Odoo
456-
pass
457-
442+
# Use Polars expressions to check for float-like strings in integer fields
443+
str_series = non_null_values.cast(pl.Utf8, strict=False)
444+
dot_mask = str_series.str.contains(r"\.", literal=True)
445+
if dot_mask.any():
446+
dot_values = str_series.filter(dot_mask)
447+
# Attempt to cast to float, fill errors with null
448+
float_series = dot_values.cast(pl.Float64, strict=False)
449+
# Check for non-null floats that are integers
450+
is_integer_float = float_series.is_not_null() & (
451+
float_series.round(0) == float_series
452+
)
453+
if is_integer_float.any():
454+
corrections_needed = True
458455
if corrections_needed:
459456
break
460457

0 commit comments

Comments
 (0)