import calendar


def derive_time_features(df, source_date_column = 'index'):
    """
    Creates time series features from datetime index

    :param df:                  Data frame representing time series
    :param source_date_column:  Column in df representing time
    :return:                    Data frame with derived features only
    """
    if (source_date_column == 'index'):
        df['date'] = df.index
        date_col = 'date'
    else:
        date_col = source_date_column

    #df['hour'] = df[date_col].dt.hour
    #df['hour'] = df[date_col].dt.minute
    df['dayofweek'] = df[date_col].dt.dayofweek
    df['dayname'] = df['dayofweek'].apply(lambda x: calendar.day_name[x])
    df['quarter'] = df[date_col].dt.quarter
    df['month'] = df[date_col].dt.month
    df['year'] = df[date_col].dt.year
    df['dayofyear'] = df[date_col].dt.dayofyear
    df['dayofmonth'] = df[date_col].dt.day
    df['weekofyear'] = df[date_col].dt.weekofyear

    X = df[[
            #'hour',
            #'minute',
            'dayofweek',
            'quarter',
            'month',
            'year',
            'dayofyear',
            'dayofmonth',
            'weekofyear'
           ]]
    return X