Skip to content

Commit b310bb8

Browse files
committed
Address FutureWarning re fillna silent no_silent_downcasting
1 parent 5c21c88 commit b310bb8

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/pytorch_tabular/categorical_encoders.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Modified https://github.com/tcassou/mlencoders/blob/master/mlencoders/base_encoder.py to suit NN encoding
55
"""Category Encoders."""
66

7-
from pandas import DataFrame, Series, unique
7+
from pandas import DataFrame, Series, option_context, unique
88

99
try:
1010
import cPickle as pickle
@@ -65,10 +65,12 @@ def transform(self, X):
6565
category_cols = X_encoded.select_dtypes(include="category").columns
6666
X_encoded[category_cols] = X_encoded[category_cols].astype("object")
6767
for col, mapping in self._mapping.items():
68-
X_encoded[col] = X_encoded[col].fillna(NAN_CATEGORY).map(mapping["value"])
68+
with option_context("future.no_silent_downcasting", True):
69+
X_encoded[col] = X_encoded[col].fillna(NAN_CATEGORY).infer_objects(copy=False).map(mapping["value"])
6970

7071
if self.handle_unseen == "impute":
71-
X_encoded[col] = X_encoded[col].fillna(self._imputed)
72+
with option_context("future.no_silent_downcasting", True):
73+
X_encoded[col] = X_encoded[col].fillna(self._imputed).infer_objects(copy=False)
7274
elif self.handle_unseen == "error":
7375
if np.unique(X_encoded[col]).shape[0] > mapping.shape[0]:
7476
raise ValueError(f"Unseen categories found in `{col}` column.")
@@ -157,7 +159,12 @@ def fit(self, X, y=None):
157159
not X[self.cols].isnull().any().any()
158160
), "`handle_missing` = `error` and missing values found in columns to encode."
159161
for col in self.cols:
160-
map = Series(unique(X[col].fillna(NAN_CATEGORY)), name=col).reset_index().rename(columns={"index": "value"})
162+
with option_context("future.no_silent_downcasting", True):
163+
map = (
164+
Series(unique(X[col].fillna(NAN_CATEGORY).infer_objects(copy=False)), name=col)
165+
.reset_index()
166+
.rename(columns={"index": "value"})
167+
)
161168
map["value"] += 1
162169
self._mapping[col] = map.set_index(col)
163170

src/pytorch_tabular/tabular_datamodule.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,15 @@ def _update_config(self, config) -> InferredConfig:
303303
else:
304304
raise ValueError(f"{config.task} is an unsupported task.")
305305
if self.train is not None:
306-
category_cols = self.train[config.categorical_cols].select_dtypes(include="category").columns
307-
self.train[category_cols] = self.train[category_cols].astype("object")
308-
categorical_cardinality = [
309-
int(x) + 1 for x in list(self.train[config.categorical_cols].fillna("NA").nunique().values)
310-
]
306+
with pd.option_context("future.no_silent_downcasting", True):
307+
category_cols = self.train[config.categorical_cols].select_dtypes(include="category").columns
308+
self.train[category_cols] = self.train[category_cols].astype("object")
309+
categorical_cardinality = [
310+
int(x) + 1
311+
for x in list(
312+
self.train[config.categorical_cols].fillna("NA").infer_objects(copy=False).nunique().values
313+
)
314+
]
311315
else:
312316
category_cols = self.train_dataset.data[config.categorical_cols].select_dtypes(include="category").columns
313317
self.train_dataset.data[category_cols] = self.train_dataset.data[category_cols].astype("object")

0 commit comments

Comments
 (0)