Exemplo n.º 1
0
def assemble_prediction_frame(frames: Dict[str, pd.DataFrame]):
    # filter non frames
    valid_frames = {
        head: frame.copy()
        for head, frame in frames.items() if frame is not None
    }

    for head, frame in valid_frames.items():
        frame.columns = pd.MultiIndex.from_product([[head],
                                                    frame.columns.to_list()])

    # join all frames and keep the order of the passed dictionary
    df = pd.concat(valid_frames.values(), axis=1, join='inner', copy=False)

    # monkey patch prediction frame
    df.map_prediction_to_target = lambda: map_prediction_to_target(
        df, PREDICTION_COLUMN_NAME, TARGET_COLUMN_NAME)
    return df
Exemplo n.º 2
0
def assemble_prediction_frame(frames: Dict[str, pd.DataFrame]):
    # filter non frames
    valid_frames = {head: (frame.as_joined_frame() if isinstance(frame, MultiFrameDecorator) else frame.copy())
                    for head, frame in frames.items() if frame is not None }

    # convert into MultiIndex column headers
    for head, frame in valid_frames.items():
        if isinstance(frame, pd.Series):
            frame = frame.to_frame()
            valid_frames[head] = frame

        frame.columns = pd.MultiIndex.from_product([[head], frame.columns.to_list()])

    # join all frames and keep the order of the passed dictionary
    df = pd.concat(valid_frames.values(), axis=1, join='inner', copy=False)

    # monkey patch prediction frame
    df.map_prediction_to_target = lambda: map_prediction_to_target(df, PREDICTION_COLUMN_NAME, TARGET_COLUMN_NAME)
    return df
Exemplo n.º 3
0
import numpy as np

from pandas_ml_common import pd

print('NUMPY VERSION', np.__version__)


def _with_multi_index(df, header):
    df = df.copy()
    df.columns = pd.MultiIndex.from_product([[header], df.columns])
    return df


def _with_multi_index_row(df, header):
    df = df.copy()
    df.index = pd.MultiIndex.from_product([[header], df.index])
    return df


DATA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".data")
TEST_FILE = os.path.join(DATA_PATH, "SPY.csv")
TEST_DF = pd.read_csv(TEST_FILE, index_col='Date', parse_dates=True)
TEST_MULTI_INDEX_DF = _with_multi_index(TEST_DF, "A").join(
    _with_multi_index(TEST_DF, "B"))
TEST_MUTLI_INDEX_ROW_DF = pd.concat([
    _with_multi_index_row(TEST_DF.tail(), "A"),
    _with_multi_index_row(TEST_DF.tail() + 1.0, "B")
],
                                    axis=0)