fix nans in colums integers

This commit is contained in:
Cayo Puigdefabregas 2024-02-28 20:10:06 +01:00
parent 4706644826
commit 6466e93bc5
1 changed files with 13 additions and 5 deletions

View File

@ -217,6 +217,7 @@ class ImportForm(forms.Form):
return data return data
def clean_file_import(self): def clean_file_import(self):
props = self.json_schema.get("properties", {})
data = self.cleaned_data["file_import"] data = self.cleaned_data["file_import"]
self.file_name = data.name self.file_name = data.name
@ -243,14 +244,21 @@ class ImportForm(forms.Form):
df[col] = df[col].dt.strftime("%Y-%m-%d") df[col] = df[col].dt.strftime("%Y-%m-%d")
# convert numbers to strings if this is indicate in schema # convert numbers to strings if this is indicate in schema
props = self.json_schema.get("properties", {}) for col in props.keys():
if col not in df.columns:
continue
for col in df.select_dtypes(include=['number']).columns: if "string" in props[col]["type"]:
type_col = props.get(col, {}).get("type")
if type_col and "string" in type_col:
df[col] = df[col].astype(str) df[col] = df[col].astype(str)
# TODO @cayop if there are a cel with nan then now is ''
# for this raison crash with df[col].astype(int)
# elif "integer" in props[col]["type"]:
# df[col] = df[col].astype(int)
# elif "number" in props[col]["type"]:
# df[col] = df[col].astype(float)
data_pd = df.to_dict(orient='index') data_pd = df.to_dict(orient='index')
if not data_pd or df.last_valid_index() is None: if not data_pd or df.last_valid_index() is None: