-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for datetime features (#122)
- Loading branch information
Showing
8 changed files
with
235 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import datetime | ||
import json | ||
|
||
class DateTimeTransformer(object): | ||
def __init__(self): | ||
self._new_columns = [] | ||
self._old_column = None | ||
self._min_datetime = None | ||
self._transforms = [] | ||
|
||
def fit(self, X, column): | ||
self._old_column = column | ||
self._min_datetime = np.min(X[column]) | ||
|
||
values = X[column].dt.year | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["year"] | ||
new_column = column +"_Year" | ||
self._new_columns += [new_column] | ||
|
||
values = X[column].dt.month | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["month"] | ||
new_column = column +"_Month" | ||
self._new_columns += [new_column] | ||
|
||
values = X[column].dt.day | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["day"] | ||
new_column = column +"_Day" | ||
self._new_columns += [new_column] | ||
|
||
values = X[column].dt.weekday | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["weekday"] | ||
new_column = column +"_WeekDay" | ||
self._new_columns += [new_column] | ||
|
||
values = X[column].dt.dayofyear | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["dayofyear"] | ||
new_column = column +"_DayOfYear" | ||
self._new_columns += [new_column] | ||
|
||
values = X[column].dt.hour | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["hour"] | ||
new_column = column +"_Hour" | ||
self._new_columns += [new_column] | ||
|
||
values = (X[column] - self._min_datetime).dt.days | ||
if len(np.unique(values)) > 1: | ||
self._transforms += ["days_diff"] | ||
new_column = column +"_Days_Diff_To_Min" | ||
self._new_columns += [new_column] | ||
|
||
def transform(self, X): | ||
column = self._old_column | ||
|
||
if "year" in self._transforms: | ||
new_column = column +"_Year" | ||
X[new_column] = X[column].dt.year | ||
|
||
if "month" in self._transforms: | ||
new_column = column +"_Month" | ||
X[new_column] = X[column].dt.month | ||
|
||
if "day" in self._transforms: | ||
new_column = column +"_Day" | ||
X[new_column] = X[column].dt.day | ||
|
||
if "weekday" in self._transforms: | ||
new_column = column +"_WeekDay" | ||
X[new_column] = X[column].dt.weekday | ||
|
||
if "dayofyear" in self._transforms: | ||
new_column = column +"_DayOfYear" | ||
X[new_column] = X[column].dt.dayofyear | ||
|
||
if "hour" in self._transforms: | ||
new_column = column +"_Hour" | ||
X[new_column] = X[column].dt.hour | ||
|
||
if "days_diff" in self._transforms: | ||
new_column = column +"_Days_Diff_To_Min" | ||
X[new_column] = (X[column] - self._min_datetime).dt.days | ||
|
||
X.drop(column, axis=1, inplace=True) | ||
return X | ||
|
||
def to_json(self): | ||
data_json = { | ||
"new_columns": list(self._new_columns), | ||
"old_column": self._old_column, | ||
"min_datetime": str(self._min_datetime), | ||
"transforms": list(self._transforms) | ||
} | ||
return data_json | ||
|
||
def from_json(self, data_json): | ||
self._new_columns = data_json.get("new_columns", None) | ||
self._old_column = data_json.get("old_column", None) | ||
d = data_json.get("min_datetime", None) | ||
self._min_datetime = None if d is None else pd.to_datetime(d) | ||
self._transforms = data_json.get("transforms", []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import unittest | ||
import tempfile | ||
import json | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from supervised.preprocessing.datetime_transformer import DateTimeTransformer | ||
|
||
class DateTimeTransformerTest(unittest.TestCase): | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
import tempfile | ||
import numpy as np | ||
import pandas as pd | ||
from supervised.preprocessing.encoding_selector import EncodingSelector | ||
from supervised.preprocessing.preprocessing_categorical import PreprocessingCategorical | ||
|
||
|
||
class CategoricalIntegersTest(unittest.TestCase): | ||
def test_selector(self): | ||
|
||
d = {"col1": ["a", "a", "c"], "col2": ["a", "b", "c"]} | ||
df = pd.DataFrame(data=d) | ||
|
||
self.assertEqual( | ||
EncodingSelector.get(df, None, "col1"), | ||
PreprocessingCategorical.CONVERT_INTEGER, | ||
) | ||
self.assertEqual( | ||
EncodingSelector.get(df, None, "col2"), | ||
PreprocessingCategorical.CONVERT_ONE_HOT, | ||
) |