Skip to content

Commit 61a68d4

Browse files
committed
explicitly cast to object to support pandas >= 2.0.3
without `future.no_silent_downcasting`
1 parent a7aa3da commit 61a68d4

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

src/pytorch_tabular/categorical_encoders.py

Lines changed: 8 additions & 11 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, option_context, unique
7+
from pandas import DataFrame, Series, unique
88

99
try:
1010
import cPickle as pickle
@@ -65,12 +65,10 @@ 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-
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"])
68+
X_encoded[col] = X_encoded[col].fillna(NAN_CATEGORY).astype("object").map(mapping["value"])
7069

7170
if self.handle_unseen == "impute":
72-
with option_context("future.no_silent_downcasting", True):
73-
X_encoded[col] = X_encoded[col].fillna(self._imputed).infer_objects(copy=False)
71+
X_encoded[col] = X_encoded[col].fillna(self._imputed).astype("object")
7472
elif self.handle_unseen == "error":
7573
if np.unique(X_encoded[col]).shape[0] > mapping.shape[0]:
7674
raise ValueError(f"Unseen categories found in `{col}` column.")
@@ -159,12 +157,11 @@ def fit(self, X, y=None):
159157
not X[self.cols].isnull().any().any()
160158
), "`handle_missing` = `error` and missing values found in columns to encode."
161159
for col in self.cols:
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-
)
160+
map = (
161+
Series(unique(X[col].fillna(NAN_CATEGORY).astype("object")), name=col)
162+
.reset_index()
163+
.rename(columns={"index": "value"})
164+
)
168165
map["value"] += 1
169166
self._mapping[col] = map.set_index(col)
170167

src/pytorch_tabular/tabular_datamodule.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,12 @@ 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-
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-
]
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
310+
for x in list(self.train[config.categorical_cols].fillna("NA").astype("object").nunique().values)
311+
]
315312
else:
316313
category_cols = self.train_dataset.data[config.categorical_cols].select_dtypes(include="category").columns
317314
self.train_dataset.data[category_cols] = self.train_dataset.data[category_cols].astype("object")

src/pytorch_tabular/tabular_model.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
from pandas import DataFrame
2727
from pytorch_lightning import seed_everything
2828
from pytorch_lightning.callbacks import RichProgressBar
29-
from pytorch_lightning.callbacks.gradient_accumulation_scheduler import (
30-
GradientAccumulationScheduler,
31-
)
29+
from pytorch_lightning.callbacks.gradient_accumulation_scheduler import GradientAccumulationScheduler
3230
from pytorch_lightning.tuner.tuning import Tuner
3331
from pytorch_lightning.utilities.model_summary import summarize
3432
from pytorch_lightning.utilities.rank_zero import rank_zero_only
@@ -48,11 +46,7 @@
4846
)
4947
from pytorch_tabular.config.config import InferredConfig
5048
from pytorch_tabular.models.base_model import BaseModel, _CaptumModel, _GenericModel
51-
from pytorch_tabular.models.common.layers.embeddings import (
52-
Embedding1dLayer,
53-
Embedding2dLayer,
54-
PreEncoded1dLayer,
55-
)
49+
from pytorch_tabular.models.common.layers.embeddings import Embedding1dLayer, Embedding2dLayer, PreEncoded1dLayer
5650
from pytorch_tabular.tabular_datamodule import TabularDatamodule
5751
from pytorch_tabular.utils import (
5852
OOMException,

0 commit comments

Comments
 (0)