pass numbers to string in pandas

This commit is contained in:
Cayo Puigdefabregas 2024-02-27 13:37:32 +01:00
parent e81837bd4e
commit fd6035aba0
1 changed files with 6 additions and 3 deletions

View File

@ -218,19 +218,22 @@ class ImportForm(forms.Form):
data = self.cleaned_data["file_import"] data = self.cleaned_data["file_import"]
self.file_name = data.name self.file_name = data.name
if not self._schema:
return data
df = pd.read_excel(data) df = pd.read_excel(data)
# convert dates to iso 8601 # convert dates to iso 8601
for col in df.select_dtypes(include='datetime').columns: for col in df.select_dtypes(include='datetime').columns:
df[col] = df[col].dt.strftime("%Y-%m-%d") df[col] = df[col].dt.strftime("%Y-%m-%d")
for col in df.select_dtypes(include=['number']).columns:
df[col] = df[col].astype(str)
data_pd = df.fillna('').to_dict() data_pd = df.fillna('').to_dict()
if not data_pd or df.last_valid_index() is None: if not data_pd or df.last_valid_index() is None:
self.exception("The file you try to import is empty!") self.exception("The file you try to import is empty!")
if not self._schema:
return data
for n in range(df.last_valid_index()+1): for n in range(df.last_valid_index()+1):
row = {} row = {}
for k in data_pd.keys(): for k in data_pd.keys():