Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/transf date #29

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
linting feature
Signed-off-by: Gillesa <arm.gilles@gmail.com>
  • Loading branch information
armgilles committed Sep 20, 2022
commit 9bba0306e22121346dd158973dfd7aa5afb54b30
17 changes: 16 additions & 1 deletion eurybia/core/smartdrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import catboost
import pandas as pd
from pandas.api.types import is_datetime64_any_dtype as is_datetime
from shapash.explainer.smart_explainer import SmartExplainer
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
Expand All @@ -22,7 +23,7 @@
from eurybia.utils.io import load_pickle, save_pickle
from eurybia.utils.model_drift import catboost_hyperparameter_init, catboost_hyperparameter_type
from eurybia.utils.statistical_tests import chisq_test, compute_js_divergence, ksmirnov_test
from eurybia.utils.utils import base_100
from eurybia.utils.utils import base_100, convert_date_col_into_multiple_col

logging.getLogger("papermill").setLevel(logging.WARNING)
logging.getLogger("blib2to3").setLevel(logging.WARNING)
Expand Down Expand Up @@ -422,6 +423,20 @@ def _analyze_consistency(self, full_validation=False, ignore_cols: list = list()
err_dtypes = [
c for c in common_cols if self.df_baseline.dtypes.map(str)[c] != self.df_current.dtypes.map(str)[c]
]

if len([column for column in self.df_current.columns if is_datetime(self.df_current[column])]) > 0:
if self.deployed_model is None:
print("""Datetime columns will be transform into df_current""")
self.df_current = convert_date_col_into_multiple_col(self.df_current)
else:
raise TypeError("df_current have datetime column. You should drop it")

if len([column for column in self.df_baseline.columns if is_datetime(self.df_baseline[column])]) > 0:
if self.deployed_model is None:
print("""Datetime columns will be transform into df_baseline""")
self.df_baseline = convert_date_col_into_multiple_col(self.df_baseline)
else:
raise TypeError("df_baseline have datetime column. You should drop it")
if len(err_dtypes) > 0:
print(
f"""The following variables have mismatching dtypes
Expand Down
31 changes: 31 additions & 0 deletions eurybia/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

import pandas as pd
from pandas.api.types import is_datetime64_any_dtype as is_datetime


def convert_string_to_int_keys(input_dict: dict) -> dict:
Expand Down Expand Up @@ -91,3 +92,33 @@ def round_to_k(x, k):
return int(new_x) # Avoid the '.0' that can mislead the user that it may be a round number
else:
return new_x


def convert_date_col_into_multiple_col(df: pd.DataFrame) -> pd.DataFrame:
"""
Transform datetime column into multiple columns
- year
- month
- day
Drop datetime column
Parameters
----------
df: pd.Dataframe
input DataFrame with datetime columns
Returns
-------
pd.Dataframe
DataFrame without datetime columns
"""

date_col_list = [column for column in df.columns if is_datetime(df[column])]

for col_date in date_col_list:
df[col_date + "_year"] = df[col_date].dt.year
df[col_date + "_month"] = df[col_date].dt.month
df[col_date + "_day"] = df[col_date].dt.day

# droping original date column
df = df.drop(col_date, axis=1)

return df