Skip to content

Commit

Permalink
fix(post-processing): handle missing values in cumulative operator (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored Jan 9, 2024
1 parent f038348 commit 6cdb2ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions superset/utils/pandas_postprocessing/cum.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def cum(
"""
columns = columns or {}
df_cum = df.loc[:, columns.keys()]
df_cum = df_cum.fillna(0)
operation = "cum" + operator
if operation not in ALLOWLIST_CUMULATIVE_FUNCTIONS or not hasattr(
df_cum, operation
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_tests/fixtures/dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
data={"label": ["x", "y", "z", "q"], "y": [1.0, 2.0, 3.0, 4.0]},
)

timeseries_with_gap_df = DataFrame(
index=to_datetime(["2019-01-01", "2019-01-02", "2019-01-05", "2019-01-07"]),
data={"label": ["x", "y", "z", "q"], "y": [1.0, 2.0, None, 4.0]},
)

timeseries_df2 = DataFrame(
index=to_datetime(["2019-01-01", "2019-01-02", "2019-01-05", "2019-01-07"]),
data={
Expand Down
14 changes: 14 additions & 0 deletions tests/unit_tests/pandas_postprocessing/test_cum.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
multiple_metrics_df,
single_metric_df,
timeseries_df,
timeseries_with_gap_df,
)
from tests.unit_tests.pandas_postprocessing.utils import series_to_list

Expand Down Expand Up @@ -77,6 +78,19 @@ def test_cum():
)


def test_cum_with_gap():
# create new column (cumsum)
post_df = pp.cum(
df=timeseries_with_gap_df,
columns={"y": "y2"},
operator="sum",
)
assert post_df.columns.tolist() == ["label", "y", "y2"]
assert series_to_list(post_df["label"]) == ["x", "y", "z", "q"]
assert series_to_list(post_df["y"]) == [1.0, 2.0, None, 4.0]
assert series_to_list(post_df["y2"]) == [1.0, 3.0, 3.0, 7.0]


def test_cum_after_pivot_with_single_metric():
pivot_df = pp.pivot(
df=single_metric_df,
Expand Down

0 comments on commit 6cdb2ff

Please sign in to comment.