2022-07-20 13:35:06 +02:00
|
|
|
import pandas as pd
|
2022-09-15 16:17:24 +02:00
|
|
|
from tqdm import tqdm
|
2022-07-20 13:35:06 +02:00
|
|
|
|
|
|
|
|
2022-08-09 18:36:40 +02:00
|
|
|
def row_to_type(row, type, **type_kwargs):
|
|
|
|
return type(**row, **type_kwargs)
|
2022-07-20 13:35:06 +02:00
|
|
|
|
|
|
|
|
2022-08-29 22:17:52 +02:00
|
|
|
def df_to_type_list(df, type, mappers=None, **type_kwargs):
|
|
|
|
if mappers is not None:
|
|
|
|
for col, mapper in mappers.items():
|
|
|
|
df[col] = df[col].map(mapper)
|
|
|
|
|
2022-07-20 13:35:06 +02:00
|
|
|
has_idx = not isinstance(df.index, pd.RangeIndex)
|
2022-08-09 18:36:40 +02:00
|
|
|
ret_list = []
|
2022-09-15 16:17:24 +02:00
|
|
|
|
2022-08-29 22:17:52 +02:00
|
|
|
for row in tqdm(df.itertuples(index=has_idx), desc=f" ├─ Processing {type.__name__}s", total=len(df)):
|
2022-08-09 18:36:40 +02:00
|
|
|
row_dict = row._asdict()
|
|
|
|
if has_idx:
|
|
|
|
row_dict["id"] = row.Index
|
|
|
|
del row_dict["Index"]
|
|
|
|
ret_list.append(row_to_type(row_dict, type, **type_kwargs))
|
|
|
|
return ret_list
|