Esempio n. 1
0
def filter_records(df: pd.DataFrame, criteria: str) -> pd.DataFrame:
    try:
        result = df[df.eval(criteria)]
        return result
    except Exception as e:
        logger.log_error(e)
        return df
Esempio n. 2
0
def plot_2d_line(df: pd.DataFrame,
                 x: str,
                 y: str,
                 color: str = None,
                 criteria: str = None) -> go.Figure:
    """
    :param df: Data set
    :param x: name of column X
    :param y: name of column Y
    :param color: name of grouping column
    :param criteria: python condition
    :return: plot figure
    """
    if criteria is not None:
        df = filter_records(df, criteria)
    if not x in df.columns:
        logger.log_error('x not found in columns')
        return
    if not y in df.columns:
        logger.log_error('y not found in columns')
        return
    if not color in df.columns and not color is None:
        logger.log_warn('color column not found')
        color = None
    return px.line(df, x=x, y=y, color=color)
Esempio n. 3
0
def box_plot(df: pd.DataFrame, x: str, y: str, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if not pd.Series([x, y]).isin(df.columns).all():
        logger.log_error('columns not founded')
        return
    return px.box(df, x=x, y=y)
Esempio n. 4
0
def pie_chart(df: pd.DataFrame, r: str, theta: str, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if not pd.Series([theta, r]).isin(df.columns).all():
        logger.log_error('columns not founded')
        return
    return px.bar_polar(df, r=r, theta=theta)
Esempio n. 5
0
def histogram(df: pd.DataFrame, x: str, bins: int = 30, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if x not in df.columns:
        logger.log_error('column not found')
        return
    return px.histogram(df, x=x, nbins=bins)
Esempio n. 6
0
def plot_3d(df: pd.DataFrame, x: str, y: str, z: str, color: str = None, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if color is None and not pd.Series([x, y, z]).isin(df.columns).all() or \
            color is not None and not pd.Series([x, y, z, color]).isin(df.columns).all():
        logger.log_error('columns not found')
        return
    return px.scatter_3d(data_frame=df, x=x, y=y, z=z, color=color)
Esempio n. 7
0
def heatmap(df: pd.DataFrame, x: str, y: str, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if not x in df.columns:
        logger.log_error('x not found in columns')
        return
    if not y in df.columns:
        logger.log_error('y not found in columns')
        return
    return px.density_heatmap(df, x=x, y=y)
Esempio n. 8
0
def filter_records(df: pd.DataFrame, criteria: str) -> pd.DataFrame:
    """
    :param df: Data Set
    :param criteria: python condition
    :return: new Data Set
    """
    try:
        result = df[df.eval(criteria)]
        return result
    except Exception as e:
        logger.log_error(e)
        return df
Esempio n. 9
0
def plot_2d(df: pd.DataFrame, x: str, y: str, color: str = None, trendline: bool = False, criteria: str = None) -> go.Figure:
    if criteria is not None:
        df = filter_records(df, criteria)
    if not x in df.columns:
        logger.log_error('x not found in columns')
        return
    if not y in df.columns:
        logger.log_error('y not found in columns')
        return
    if not color in df.columns and not color is None:
        logger.log_warn('color column not found')
        color = None
    if trendline:
        trendline = 'ols'
    return px.scatter(df, x=x, y=y, color=color, trendline=trendline)
Esempio n. 10
0
def box_plot(df: pd.DataFrame,
             x: str,
             y: str,
             criteria: str = None) -> go.Figure:
    """
    :param df: Data Set
    :param x: name of column X
    :param y: name of column Y
    :param criteria: python condition
    :return: plot figure
    """
    if criteria is not None:
        df = filter_records(df, criteria)
    if not pd.Series([x, y]).isin(df.columns).all():
        logger.log_error('columns not founded')
        return
    return px.box(df, x=x, y=y)
Esempio n. 11
0
def histogram(df: pd.DataFrame,
              x: str,
              bins: int = 30,
              criteria: str = None) -> go.Figure:
    """
    :param df: Data Set
    :param x: column name for plotting (must be numeric)
    :param bins: data bins
    :param criteria: python condition
    :return: histogram figure
    """
    if criteria is not None:
        df = filter_records(df, criteria)
    if x not in df.columns:
        logger.log_error('column not found')
        return
    return px.histogram(df, x=x, nbins=bins)
Esempio n. 12
0
def plot_3d(df: pd.DataFrame,
            x: str,
            y: str,
            z: str,
            color: str = None,
            criteria: str = None) -> go.Figure:
    """
    :param df: Data Set
    :param x: name of column X
    :param y: name of column Y
    :param z: name of column Z
    :param color: name of grouping column
    :param criteria: python condition
    :return: plot figure
    """
    if criteria is not None:
        df = filter_records(df, criteria)
    if color is None and not pd.Series([x, y, z]).isin(df.columns).all() or \
            color is not None and not pd.Series([x, y, z, color]).isin(df.columns).all():
        logger.log_error('columns not found')
        return
    return px.scatter_3d(data_frame=df, x=x, y=y, z=z, color=color)