def test_transformer(self): d = { "col1": [ "2020/06/01", "2020/06/02", "2020/06/03", "2021/06/01", "2022/06/01", ] } df = pd.DataFrame(data=d) df["col1"] = pd.to_datetime(df["col1"]) df_org = df.copy() transf = DateTimeTransformer() transf.fit(df, "col1") df = transf.transform(df) self.assertTrue(df.shape[0] == 5) self.assertTrue("col1" not in df.columns) self.assertTrue("col1_Year" in df.columns) transf2 = DateTimeTransformer() transf2.from_json(transf.to_json()) df2 = transf2.transform(df_org) self.assertTrue("col1" not in df2.columns) self.assertTrue("col1_Year" in df2.columns)
def from_json(self, data_json): self._params = data_json.get("params", self._params) if "remove_columns" in data_json: self._remove_columns = data_json.get("remove_columns", []) if "missing_values" in data_json: self._missing_values = [] for mv_data in data_json["missing_values"]: mv = PreprocessingMissingValues() mv.from_json(mv_data) self._missing_values += [mv] if "categorical" in data_json: self._categorical = [] for cat_data in data_json["categorical"]: cat = PreprocessingCategorical() cat.from_json(cat_data) self._categorical += [cat] if "datetime_transforms" in data_json: self._datetime_transforms = [] for dtt_params in data_json["datetime_transforms"]: dtt = DateTimeTransformer() dtt.from_json(dtt_params) self._datetime_transforms += [dtt] if "text_transforms" in data_json: self._text_transforms = [] for tt_params in data_json["text_transforms"]: tt = TextTransformer() tt.from_json(tt_params) self._text_transforms += [tt] if "golden_features" in data_json: self._golden_features = GoldenFeaturesTransformer() self._golden_features.from_json(data_json["golden_features"]) if "scale" in data_json: self._scale = [] for scale_data in data_json["scale"]: sc = Scale() sc.from_json(scale_data) self._scale += [sc] if "categorical_y" in data_json: if "new_columns" in data_json["categorical_y"]: self._categorical_y = LabelBinarizer() else: self._categorical_y = LabelEncoder() self._categorical_y.from_json(data_json["categorical_y"]) if "scale_y" in data_json: self._scale_y = Scale() self._scale_y.from_json(data_json["scale_y"]) if "ml_task" in data_json: self._params["ml_task"] = data_json["ml_task"] self._add_random_feature = data_json.get("add_random_feature", False) self._drop_features = data_json.get("drop_features", [])