mirror of https://github.com/apache/superset.git
fix: missing mulitiple metrics on pivot operator (#16026)
* fix: missing mulitiple metrics on pivot operator * code smell
This commit is contained in:
parent
a889cb3505
commit
3f57ea76db
|
@ -271,8 +271,9 @@ def pivot( # pylint: disable=too-many-arguments
|
||||||
series_set = set()
|
series_set = set()
|
||||||
if not drop_missing_columns and columns:
|
if not drop_missing_columns and columns:
|
||||||
for row in df[columns].itertuples():
|
for row in df[columns].itertuples():
|
||||||
metrics_and_series = tuple(aggfunc.keys()) + tuple(row[1:])
|
for metric in aggfunc.keys():
|
||||||
series_set.add(str(metrics_and_series))
|
series_set.add(str(tuple([metric]) + tuple(row[1:])))
|
||||||
|
|
||||||
df = df.pivot_table(
|
df = df.pivot_table(
|
||||||
values=aggfunc.keys(),
|
values=aggfunc.keys(),
|
||||||
index=index,
|
index=index,
|
||||||
|
|
|
@ -258,6 +258,7 @@ class TestPostProcessing(SupersetTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_pivot_eliminate_cartesian_product_columns(self):
|
def test_pivot_eliminate_cartesian_product_columns(self):
|
||||||
|
# single metric
|
||||||
mock_df = DataFrame(
|
mock_df = DataFrame(
|
||||||
{
|
{
|
||||||
"dttm": to_datetime(["2019-01-01", "2019-01-01"]),
|
"dttm": to_datetime(["2019-01-01", "2019-01-01"]),
|
||||||
|
@ -277,6 +278,33 @@ class TestPostProcessing(SupersetTestCase):
|
||||||
self.assertEqual(list(df.columns), ["dttm", "0, 0", "1, 1"])
|
self.assertEqual(list(df.columns), ["dttm", "0, 0", "1, 1"])
|
||||||
self.assertTrue(np.isnan(df["1, 1"][0]))
|
self.assertTrue(np.isnan(df["1, 1"][0]))
|
||||||
|
|
||||||
|
# multiple metrics
|
||||||
|
mock_df = DataFrame(
|
||||||
|
{
|
||||||
|
"dttm": to_datetime(["2019-01-01", "2019-01-01"]),
|
||||||
|
"a": [0, 1],
|
||||||
|
"b": [0, 1],
|
||||||
|
"metric": [9, np.NAN],
|
||||||
|
"metric2": [10, 11],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
df = proc.pivot(
|
||||||
|
df=mock_df,
|
||||||
|
index=["dttm"],
|
||||||
|
columns=["a", "b"],
|
||||||
|
aggregates={
|
||||||
|
"metric": {"operator": "mean"},
|
||||||
|
"metric2": {"operator": "mean"},
|
||||||
|
},
|
||||||
|
drop_missing_columns=False,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
list(df.columns),
|
||||||
|
["dttm", "metric, 0, 0", "metric, 1, 1", "metric2, 0, 0", "metric2, 1, 1"],
|
||||||
|
)
|
||||||
|
self.assertTrue(np.isnan(df["metric, 1, 1"][0]))
|
||||||
|
|
||||||
def test_aggregate(self):
|
def test_aggregate(self):
|
||||||
aggregates = {
|
aggregates = {
|
||||||
"asc sum": {"column": "asc_idx", "operator": "sum"},
|
"asc sum": {"column": "asc_idx", "operator": "sum"},
|
||||||
|
|
Loading…
Reference in New Issue