Skip to content

Commit

Permalink
fix: Box Plot Chart throws an error when the average (AVG) / SUM is b…
Browse files Browse the repository at this point in the history
…eing calculated on the Metrics (#20235)

* fix: Box Plot Chart throws an error when the average (AVG) / SUM is being calculated on the Metrics

* add test

(cherry picked from commit 8638f59)
  • Loading branch information
diegomedina248 authored and michael-s-molina committed Jun 24, 2022
1 parent d512e89 commit 4073b58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion superset/utils/pandas_postprocessing/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import numpy as np
from flask_babel import gettext as _
from pandas import DataFrame, Series
from pandas import DataFrame, Series, to_numeric

from superset.exceptions import InvalidPostProcessingError
from superset.utils.core import PostProcessingBoxplotWhiskerType
Expand Down Expand Up @@ -122,4 +122,11 @@ def outliers(series: Series) -> Set[float]:
for operator_name, operator in operators.items()
for metric in metrics
}

# nanpercentile needs numeric values, otherwise the isnan function
# that's used in the underlying function will fail
for column in metrics:
if df.dtypes[column] == np.object:
df[column] = to_numeric(df[column], errors="coerce")

return aggregate(df, groupby=groupby, aggregates=aggregates)
25 changes: 25 additions & 0 deletions tests/unit_tests/pandas_postprocessing/test_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,28 @@ def test_boxplot_percentile_incorrect_params():
metrics=["cars"],
percentiles=[10, 90, 10],
)


def test_boxplot_type_coercion():
df = names_df
df["cars"] = df["cars"].astype(str)
df = boxplot(
df=df,
groupby=["region"],
whisker_type=PostProcessingBoxplotWhiskerType.TUKEY,
metrics=["cars"],
)

columns = {column for column in df.columns}
assert columns == {
"cars__mean",
"cars__median",
"cars__q1",
"cars__q3",
"cars__max",
"cars__min",
"cars__count",
"cars__outliers",
"region",
}
assert len(df) == 4

0 comments on commit 4073b58

Please sign in to comment.