This is an automated email from the ASF dual-hosted git repository.
yongjiezhao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new d7dd4119d4 fix: time comparision (#19659)
d7dd4119d4 is described below
commit d7dd4119d4277dcd4682631de154b6aae27cbe69
Author: Yongjie Zhao <[email protected]>
AuthorDate: Tue Apr 12 18:19:22 2022 +0800
fix: time comparision (#19659)
---
superset/utils/pandas_postprocessing/compare.py | 7 ++--
.../pandas_postprocessing/test_compare.py | 40 +++++++++++-----------
2 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/superset/utils/pandas_postprocessing/compare.py
b/superset/utils/pandas_postprocessing/compare.py
index 18a66cec13..2394aaa026 100644
--- a/superset/utils/pandas_postprocessing/compare.py
+++ b/superset/utils/pandas_postprocessing/compare.py
@@ -65,13 +65,12 @@ def compare( # pylint: disable=too-many-arguments
c_df = df.loc[:, [c_col]]
c_df.rename(columns={c_col: "__intermediate"}, inplace=True)
if compare_type == PandasPostprocessingCompare.DIFF:
- diff_df = c_df - s_df
+ diff_df = s_df - c_df
elif compare_type == PandasPostprocessingCompare.PCT:
- #
https://en.wikipedia.org/wiki/Relative_change_and_difference#Percentage_change
- diff_df = ((c_df - s_df) / s_df).astype(float).round(precision)
+ diff_df = ((s_df - c_df) / c_df).astype(float).round(precision)
else:
# compare_type == "ratio"
- diff_df = (c_df / s_df).astype(float).round(precision)
+ diff_df = (s_df / c_df).astype(float).round(precision)
diff_df.rename(
columns={
diff --git a/tests/unit_tests/pandas_postprocessing/test_compare.py
b/tests/unit_tests/pandas_postprocessing/test_compare.py
index 970fa42f96..4f742bae16 100644
--- a/tests/unit_tests/pandas_postprocessing/test_compare.py
+++ b/tests/unit_tests/pandas_postprocessing/test_compare.py
@@ -44,9 +44,9 @@ def test_compare_diff():
"""
label y z difference__y__z
2019-01-01 x 2.0 2.0 0.0
- 2019-01-02 y 2.0 4.0 2.0
- 2019-01-05 z 2.0 10.0 8.0
- 2019-01-07 q 2.0 8.0 6.0
+ 2019-01-02 y 2.0 4.0 -2.0
+ 2019-01-05 z 2.0 10.0 -8.0
+ 2019-01-07 q 2.0 8.0 -6.0
"""
assert post_df.equals(
pd.DataFrame(
@@ -55,7 +55,7 @@ def test_compare_diff():
"label": ["x", "y", "z", "q"],
"y": [2.0, 2.0, 2.0, 2.0],
"z": [2.0, 4.0, 10.0, 8.0],
- "difference__y__z": [0.0, 2.0, 8.0, 6.0],
+ "difference__y__z": [0.0, -2.0, -8.0, -6.0],
},
)
)
@@ -73,7 +73,7 @@ def test_compare_diff():
index=timeseries_df2.index,
data={
"label": ["x", "y", "z", "q"],
- "difference__y__z": [0.0, 2.0, 8.0, 6.0],
+ "difference__y__z": [0.0, -2.0, -8.0, -6.0],
},
)
)
@@ -90,9 +90,9 @@ def test_compare_percentage():
"""
label y z percentage__y__z
2019-01-01 x 2.0 2.0 0.0
- 2019-01-02 y 2.0 4.0 1.0
- 2019-01-05 z 2.0 10.0 4.0
- 2019-01-07 q 2.0 8.0 3.0
+ 2019-01-02 y 2.0 4.0 -0.50
+ 2019-01-05 z 2.0 10.0 -0.80
+ 2019-01-07 q 2.0 8.0 -0.75
"""
assert post_df.equals(
pd.DataFrame(
@@ -101,7 +101,7 @@ def test_compare_percentage():
"label": ["x", "y", "z", "q"],
"y": [2.0, 2.0, 2.0, 2.0],
"z": [2.0, 4.0, 10.0, 8.0],
- "percentage__y__z": [0.0, 1.0, 4.0, 3.0],
+ "percentage__y__z": [0.0, -0.50, -0.80, -0.75],
},
)
)
@@ -117,10 +117,10 @@ def test_compare_ratio():
)
"""
label y z ratio__y__z
- 2019-01-01 x 2.0 2.0 1.0
- 2019-01-02 y 2.0 4.0 2.0
- 2019-01-05 z 2.0 10.0 5.0
- 2019-01-07 q 2.0 8.0 4.0
+ 2019-01-01 x 2.0 2.0 1.00
+ 2019-01-02 y 2.0 4.0 0.50
+ 2019-01-05 z 2.0 10.0 0.20
+ 2019-01-07 q 2.0 8.0 0.25
"""
assert post_df.equals(
pd.DataFrame(
@@ -129,7 +129,7 @@ def test_compare_ratio():
"label": ["x", "y", "z", "q"],
"y": [2.0, 2.0, 2.0, 2.0],
"z": [2.0, 4.0, 10.0, 8.0],
- "ratio__y__z": [1.0, 2.0, 5.0, 4.0],
+ "ratio__y__z": [1.00, 0.50, 0.20, 0.25],
},
)
)
@@ -209,14 +209,14 @@ def test_compare_after_pivot():
difference__count_metric__sum_metric
country UK US
dttm
- 2019-01-01 4 4
- 2019-01-02 4 4
+ 2019-01-01 -4 -4
+ 2019-01-02 -4 -4
"""
flat_df = pp.flatten(compared_df)
"""
dttm difference__count_metric__sum_metric, UK
difference__count_metric__sum_metric, US
- 0 2019-01-01 4
4
- 1 2019-01-02 4
4
+ 0 2019-01-01 -4
-4
+ 1 2019-01-02 -4
-4
"""
assert flat_df.equals(
pd.DataFrame(
@@ -224,10 +224,10 @@ def test_compare_after_pivot():
"dttm": pd.to_datetime(["2019-01-01", "2019-01-02"]),
FLAT_COLUMN_SEPARATOR.join(
["difference__count_metric__sum_metric", "UK"]
- ): [4, 4],
+ ): [-4, -4],
FLAT_COLUMN_SEPARATOR.join(
["difference__count_metric__sum_metric", "US"]
- ): [4, 4],
+ ): [-4, -4],
}
)
)