|
4 | 4 | # Modified https://github.com/tcassou/mlencoders/blob/master/mlencoders/base_encoder.py to suit NN encoding |
5 | 5 | """Category Encoders.""" |
6 | 6 |
|
7 | | -from pandas import DataFrame, Series, unique |
| 7 | +from pandas import DataFrame, Series, option_context, unique |
8 | 8 |
|
9 | 9 | try: |
10 | 10 | import cPickle as pickle |
@@ -65,10 +65,12 @@ def transform(self, X): |
65 | 65 | category_cols = X_encoded.select_dtypes(include="category").columns |
66 | 66 | X_encoded[category_cols] = X_encoded[category_cols].astype("object") |
67 | 67 | 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"]) |
69 | 70 |
|
70 | 71 | 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) |
72 | 74 | elif self.handle_unseen == "error": |
73 | 75 | if np.unique(X_encoded[col]).shape[0] > mapping.shape[0]: |
74 | 76 | raise ValueError(f"Unseen categories found in `{col}` column.") |
@@ -157,7 +159,12 @@ def fit(self, X, y=None): |
157 | 159 | not X[self.cols].isnull().any().any() |
158 | 160 | ), "`handle_missing` = `error` and missing values found in columns to encode." |
159 | 161 | 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 | + ) |
161 | 168 | map["value"] += 1 |
162 | 169 | self._mapping[col] = map.set_index(col) |
163 | 170 |
|
|
0 commit comments