|
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, option_context, unique |
| 7 | +from pandas import DataFrame, Series, unique |
8 | 8 |
|
9 | 9 | try: |
10 | 10 | import cPickle as pickle |
@@ -65,12 +65,10 @@ 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 | | - 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"]) |
70 | 69 |
|
71 | 70 | 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") |
74 | 72 | elif self.handle_unseen == "error": |
75 | 73 | if np.unique(X_encoded[col]).shape[0] > mapping.shape[0]: |
76 | 74 | raise ValueError(f"Unseen categories found in `{col}` column.") |
@@ -159,12 +157,11 @@ def fit(self, X, y=None): |
159 | 157 | not X[self.cols].isnull().any().any() |
160 | 158 | ), "`handle_missing` = `error` and missing values found in columns to encode." |
161 | 159 | 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 | + ) |
168 | 165 | map["value"] += 1 |
169 | 166 | self._mapping[col] = map.set_index(col) |
170 | 167 |
|
|
0 commit comments