Skip to content

Commit 62ef97a

Browse files
author
Thierry RAMORASOAVINA
committed
Avoid implicit type downgrading for pandas Series or DataFrame when using replace()
- An explicit call to `infer_objects()` forces the inference to the actual dtype
1 parent 308ca77 commit 62ef97a

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

tests/test_sklearn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,9 @@ def _create_train_test_multitable_dataframe(cls, transform_for_regression=False)
11351135
dataset_name, "SpliceJunction", "SpliceJunctionDNA"
11361136
)
11371137
if transform_for_regression:
1138-
root_table_data.replace({"Class": {"EI": 1, "IE": 2, "N": 3}}, inplace=True)
1138+
root_table_data = root_table_data.replace(
1139+
{"Class": {"EI": 1, "IE": 2, "N": 3}}
1140+
).infer_objects() # this call forces the inference to the actual dtype
11391141
root_train_data, root_test_data = KhiopsTestHelper.prepare_data(
11401142
root_table_data, "Class"
11411143
)

tests/test_sklearn_output_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def test_classifier_output_types(self):
8484
"int": y,
8585
"int binary": y_bin,
8686
"float": y.astype(float),
87-
"bool": y.replace({0: True, 1: True, 2: False}),
87+
# the call to `infer_objects()` forces the inference to the actual dtype
88+
"bool": y.replace({0: True, 1: True, 2: False}).infer_objects(),
8889
"string": self._replace(y, {0: "se", 1: "vi", 2: "ve"}),
8990
"string binary": self._replace(y_bin, {0: "vi_or_se", 1: "ve"}),
9091
"int as string": self._replace(y, {0: "8", 1: "9", 2: "10"}),

0 commit comments

Comments
 (0)