def train_test_split_by_last_days(stock_df, periods_per_day=7,
    num_test_days=90, num_train_days=180, verbose=1, plot=True):
    """Takes the last num_test_days of the time index to use as testing data, and take shte num_Trian_days prior to that date
    as the training data."""
    from IPython.display import display
    import matplotlib.pyplot as plt
    if verbose > 1:
        print(f'Data index (freq={stock_df.index.freq}')
        print(f'index[0] = {stock_df.index[0]}, index[-1]={stock_df.index[-1]}'
            )
    day_freq = periods_per_day
    start_train_day = stock_df.index[-1] - (num_train_days + num_test_days
        ) * day_freq
    last_train_day = stock_df.index[-1] - num_test_days * day_freq
    train_data = stock_df.loc[start_train_day:last_train_day]
    test_data = stock_df.loc[last_train_day:]
    if verbose > 0:
        print(f'Data split on index:\t{last_train_day}:')
        print(
            f'training dates:\t{train_data.index[0]} \t {train_data.index[-1]}.'
            )
        print(f'test dates:\t{test_data.index[0]} \t {test_data.index[-1]}.')
    if verbose > 1:
        display(train_data.head(3).style.set_caption('Training Data'))
        display(test_data.head(3).style.set_caption('Test Data'))
    if plot == True:
        if 'price' in stock_df.columns:
            plot_col = 'price'
        elif 'price_labels' in stock_df.columns:
            plot_col = 'price_labels'
        fig = plt.figure(figsize=(8, 4))
        train_data[plot_col].plot(label='Training')
        test_data[plot_col].plot(label='Test')
        plt.title('Training and Test Data for S&P500')
        plt.ylabel('Price')
        plt.xlabel('Trading Date/Hour')
        plt.legend()
        plt.show()
    return train_data, test_data
