Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit e883aca

Browse files
fix: Fix error caused when input data is mixed with datetime and string types (#7)
* fix: Fix error caused when input data is mixed with datetime and string types Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix: Fix error caused when input data is mixed with datetime and string types Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix: Fix error caused when input data is mixed with datetime and string types Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix:Modifying Source Code Formatting Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix:Reflect Review Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix: Remove Unnecessary Imports Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> * fix: Reflect the point Signed-off-by: tashiro akira <fj1755jk@fujitsu.com> --------- Signed-off-by: tashiro akira <fj1755jk@fujitsu.com>
1 parent 53d0c1f commit e883aca

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

sapientml_preprocess/generator.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,36 @@ def generate_code(self, dataset: Dataset, task: Task) -> Tuple[Dataset, Code]:
265265
only_str = col + "__str"
266266
only_num = col + "__num"
267267
df[only_str] = np.where(pd.to_numeric(df[col], errors="coerce").isnull(), df[col], np.nan)
268+
df[only_str] = np.where(df[only_str].notnull(), df[only_str].astype(str), np.nan)
268269
# without .astype(float), cannot recongnize as `int` or `float`, leading to generate inappropriate code snippet
269270
df[only_num] = np.where(pd.to_numeric(df[col], errors="coerce").isnull(), np.nan, df[col]).astype(float)
270271
df = df.drop(col, axis=1)
271272
if cols_numeric_and_string:
272273
tpl = template_env.get_template("handle_mixed_typed_columns.py.jinja")
273-
code.validation += _render(tpl, training=True, test=True, cols_numeric_and_string=cols_numeric_and_string)
274-
code.test += _render(tpl, training=True, test=True, cols_numeric_and_string=cols_numeric_and_string)
275-
code.train += _render(tpl, training=True, test=False, cols_numeric_and_string=cols_numeric_and_string)
276-
code.predict += _render(tpl, training=False, test=True, cols_numeric_and_string=cols_numeric_and_string)
274+
code.validation += _render(
275+
tpl,
276+
training=True,
277+
test=True,
278+
cols_numeric_and_string=cols_numeric_and_string
279+
)
280+
code.test += _render(
281+
tpl,
282+
training=True,
283+
test=True,
284+
cols_numeric_and_string=cols_numeric_and_string
285+
)
286+
code.train += _render(
287+
tpl,
288+
training=True,
289+
test=False,
290+
cols_numeric_and_string=cols_numeric_and_string
291+
)
292+
code.predict += _render(
293+
tpl,
294+
training=False,
295+
test=True,
296+
cols_numeric_and_string=cols_numeric_and_string
297+
)
277298

278299
# meta features must be calculated after replacing inf with nan,
279300
# becuase the replaced nan must be preprocessed in the generated code.

sapientml_preprocess/templates/handle_mixed_typed_columns.py.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ cols_numeric_and_string = {{ cols_numeric_and_string}}
44
for col in cols_numeric_and_string:
55
{% if training %}
66
train_dataset[col + '__str'] = np.where(pd.to_numeric(train_dataset[col], errors='coerce').isnull(), train_dataset[col], np.nan)
7+
train_dataset[col + '__str'] = np.where(train_dataset[col + '__str'].notnull(), train_dataset[col + '__str'].astype(str), np.nan)
78
train_dataset[col + '__num'] = np.where(pd.to_numeric(train_dataset[col], errors='coerce').isnull(), np.nan, train_dataset[col]).astype(float)
89
train_dataset = train_dataset.drop(col, axis=1)
910
{% endif %}
1011
{% if test %}
1112
test_dataset[col + '__str'] = np.where(pd.to_numeric(test_dataset[col], errors='coerce').isnull(), test_dataset[col], np.nan)
13+
test_dataset[col + '__str'] = np.where(test_dataset[col + '__str'].notnull(), test_dataset[col + '__str'].astype(str), np.nan)
1214
test_dataset[col + '__num'] = np.where(pd.to_numeric(test_dataset[col], errors='coerce').isnull(), np.nan, test_dataset[col]).astype(float)
1315
test_dataset = test_dataset.drop(col, axis=1)
1416
{% endif %}

0 commit comments

Comments
 (0)