Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: time comparision #19659

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions superset/utils/pandas_postprocessing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
40 changes: 20 additions & 20 deletions tests/unit_tests/pandas_postprocessing/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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],
},
)
)
Expand All @@ -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],
},
)
)
Expand All @@ -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(
Expand All @@ -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],
},
)
)
Expand All @@ -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(
Expand All @@ -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],
},
)
)
Expand Down Expand Up @@ -209,25 +209,25 @@ 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(
data={
"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],
}
)
)