Exemple #1
0
def pivot_table(
    df: pd.DataFrame,
    form_data: Dict[str, Any],
    datasource: Optional["BaseDatasource"] = None,
) -> pd.DataFrame:
    """
    Pivot table (v1).
    """
    verbose_map = datasource.data["verbose_map"] if datasource else None
    if form_data.get("granularity") == "all" and DTTM_ALIAS in df:
        del df[DTTM_ALIAS]

    # v1 func names => v2 func names
    func_map = {
        "sum": "Sum",
        "mean": "Average",
        "min": "Minimum",
        "max": "Maximum",
        "std": "Sample Standard Deviation",
        "var": "Sample Variance",
    }

    return pivot_df(
        df,
        rows=get_column_names(form_data.get("groupby"), verbose_map),
        columns=get_column_names(form_data.get("columns"), verbose_map),
        metrics=get_metric_names(form_data["metrics"], verbose_map),
        aggfunc=func_map.get(form_data.get("pandas_aggfunc", "sum"), "Sum"),
        transpose_pivot=bool(form_data.get("transpose_pivot")),
        combine_metrics=bool(form_data.get("combine_metric")),
        show_rows_total=bool(form_data.get("pivot_margins")),
        show_columns_total=bool(form_data.get("pivot_margins")),
        apply_metrics_on_rows=False,
    )
Exemple #2
0
def test_get_column_names():
    assert get_column_names([STR_COLUMN, SQL_ADHOC_COLUMN]) == [
        "my_column",
        "My Adhoc Column",
    ]
    assert get_column_names(
        [STR_COLUMN, SQL_ADHOC_COLUMN],
        {"my_column": "My Column"},
    ) == ["My Column", "My Adhoc Column"]
Exemple #3
0
def pivot_table_v2(
    df: pd.DataFrame,
    form_data: Dict[str, Any],
    datasource: Optional["BaseDatasource"] = None,
) -> pd.DataFrame:
    """
    Pivot table v2.
    """
    verbose_map = datasource.data["verbose_map"] if datasource else None
    if form_data.get("granularity_sqla") == "all" and DTTM_ALIAS in df:
        del df[DTTM_ALIAS]

    return pivot_df(
        df,
        rows=get_column_names(form_data.get("groupbyRows"), verbose_map),
        columns=get_column_names(form_data.get("groupbyColumns"), verbose_map),
        metrics=get_metric_names(form_data["metrics"], verbose_map),
        aggfunc=form_data.get("aggregateFunction", "Sum"),
        transpose_pivot=bool(form_data.get("transposePivot")),
        combine_metrics=bool(form_data.get("combineMetric")),
        show_rows_total=bool(form_data.get("rowTotals")),
        show_columns_total=bool(form_data.get("colTotals")),
        apply_metrics_on_rows=form_data.get("metricsLayout") == "ROWS",
    )
Exemple #4
0
 def column_names(self) -> List[str]:
     """Return column names (labels). Gives priority to groupbys if both groupbys
     and metrics are non-empty, otherwise returns column labels."""
     return get_column_names(self.columns)