def get_datetime_col()

in fclib/fclib/feature_engineering/feature_utils.py [0:0]


def get_datetime_col(df, datetime_colname):
    """
    Helper function for extracting the datetime column as datetime type from
    a data frame.

    Args:
        df: pandas DataFrame containing the column to convert
        datetime_colname: name of the column to be converted

    Returns:
        pandas.Series: converted column

    Raises:
        Exception: if datetime_colname does not exist in the dateframe df.
        Exception: if datetime_colname cannot be converted to datetime type.
    """
    if datetime_colname in df.index.names:
        datetime_col = df.index.get_level_values(datetime_colname)
    elif datetime_colname in df.columns:
        datetime_col = df[datetime_colname]
    else:
        raise Exception("Column or index {0} does not exist in the data " "frame".format(datetime_colname))

    if not is_datetime_like(datetime_col):
        datetime_col = pd.to_datetime(df[datetime_colname])
    return datetime_col