Skip to content

Commit

Permalink
fix(pivot): default missing series to NULL_STRING (#14748)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored May 21, 2021
1 parent c831655 commit 7c17b1a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions superset/utils/pandas_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from geopy.point import Point
from pandas import DataFrame, NamedAgg, Series, Timestamp

from superset.constants import NULL_STRING
from superset.exceptions import QueryObjectValidationError
from superset.utils.core import (
DTTM_ALIAS,
Expand Down Expand Up @@ -214,7 +215,7 @@ def pivot( # pylint: disable=too-many-arguments
aggregates: Dict[str, Dict[str, Any]],
columns: Optional[List[str]] = None,
metric_fill_value: Optional[Any] = None,
column_fill_value: Optional[str] = None,
column_fill_value: Optional[str] = NULL_STRING,
drop_missing_columns: Optional[bool] = True,
combine_value_with_metric: bool = False,
marginal_distributions: Optional[bool] = None,
Expand All @@ -228,7 +229,9 @@ def pivot( # pylint: disable=too-many-arguments
:param index: Columns to group by on the table index (=rows)
:param columns: Columns to group by on the table columns
:param metric_fill_value: Value to replace missing values with
:param column_fill_value: Value to replace missing pivot columns with
:param column_fill_value: Value to replace missing pivot columns with. By default
replaces missing values with "<NULL>". Set to `None` to remove columns
with missing values.
:param drop_missing_columns: Do not include columns whose entries are all missing
:param combine_value_with_metric: Display metrics side by side within each column,
as opposed to each column being displayed side by side for each metric.
Expand All @@ -250,7 +253,7 @@ def pivot( # pylint: disable=too-many-arguments
_("Pivot operation must include at least one aggregate")
)

if column_fill_value:
if columns and column_fill_value:
df[columns] = df[columns].fillna(value=column_fill_value)

aggregate_funcs = _get_aggregate_funcs(df, aggregates)
Expand Down
15 changes: 15 additions & 0 deletions tests/pandas_postprocessing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ def test_pivot_fill_values(self):
)
self.assertEqual(df.sum()[1], 382)

def test_pivot_fill_column_values(self):
"""
Make sure pivot witn null column names returns correct DataFrame
"""
df_copy = categories_df.copy()
df_copy["category"] = None
df = proc.pivot(
df=df_copy,
index=["name"],
columns=["category"],
aggregates={"idx_nulls": {"operator": "sum"}},
)
assert len(df) == 101
assert df.columns.tolist() == ["name", "<NULL>"]

def test_pivot_exceptions(self):
"""
Make sure pivot raises correct Exceptions
Expand Down

0 comments on commit 7c17b1a

Please sign in to comment.