Skip to content

Instantly share code, notes, and snippets.

@AVJdataminer
Last active April 24, 2020 03:52
Show Gist options
  • Save AVJdataminer/9a1373a5327a6d9d69c99ff2ed491aa6 to your computer and use it in GitHub Desktop.
Save AVJdataminer/9a1373a5327a6d9d69c99ff2ed491aa6 to your computer and use it in GitHub Desktop.
from datetime import datetime,timedelta
def dt_splitter(date_col, X, y, test_size):
xw_date=pd.DataFrame(X).merge(date_col,left_index=True, right_index=True)
ad = (max(xw_date.date)- min(xw_date.date)).days*test_size
split_date = min(xw_date.date) + timedelta(days=ad)
X_train = xw_date.loc[xw_date['date'] <= split_date].drop(['date'], axis=1).values
X_test = xw_date.loc[xw_date['date'] > split_date].drop(['date'], axis=1).values
yw_date=pd.DataFrame(y).merge(date_col,left_index=True, right_index=True)
y_train=yw_date.loc[yw_date['date'] <= split_date].drop(['date'], axis=1).values
y_test=yw_date.loc[yw_date['date'] > split_date].drop(['date'], axis=1).values
return X_train, X_test, y_train, y_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment