You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2025/01/09 18:54:49 UTC

(superset) 01/01: add get_loc test

This is an automated email from the ASF dual-hosted git repository.

elizabeth pushed a commit to branch elizabeth/repro_get_loc_error
in repository https://gitbox.apache.org/repos/asf/superset.git

commit bc4cdf7350bf8fa981e81fdff39f06ecd8ad095c
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Thu Jan 9 10:54:22 2025 -0800

    add get_loc test
---
 tests/unit_tests/charts/test_client_processing.py | 101 ++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/tests/unit_tests/charts/test_client_processing.py b/tests/unit_tests/charts/test_client_processing.py
index e35229554b..f8435dddae 100644
--- a/tests/unit_tests/charts/test_client_processing.py
+++ b/tests/unit_tests/charts/test_client_processing.py
@@ -407,6 +407,107 @@ def test_pivot_df_single_row_two_metrics():
     )
 
 
+def test_pivot_df_single_row_two_metrics_multiindex():
+    """
+    Pivot table when a single column and 2 metrics are selected with multi-index.
+    """
+    df = pd.DataFrame.from_dict(
+        {
+            "gender": {0: "girl", 1: "boy"},
+            "age": {0: 10, 1: 12},
+            "SUM(num)": {0: 118065, 1: 47123},
+            "MAX(num)": {0: 2588, 1: 1280},
+        }
+    )
+    df.set_index(["gender", "age"], inplace=True)
+
+    assert (
+        df.to_markdown()
+        == """
+|              |   SUM(num) |   MAX(num) |
+|:-------------|-----------:|-----------:|
+| ('girl', 10) |     118065 |       2588 |
+| ('boy', 12)  |      47123 |       1280 |
+        """.strip()
+    )
+
+    pivoted = pivot_df(
+        df,
+        rows=["gender", "age"],
+        columns=[],
+        metrics=["SUM(num)", "MAX(num)"],
+        aggfunc="Sum",
+        transpose_pivot=False,
+        combine_metrics=False,
+        show_rows_total=False,
+        show_columns_total=False,
+        apply_metrics_on_rows=False,
+    )
+
+    assert (
+        pivoted.to_markdown()
+        == """
+|              |   ('SUM(num)',) |   ('MAX(num)',) |
+|:-------------|----------------:|----------------:|
+| ('boy', 12)  |           47123 |            1280 |
+| ('girl', 10) |          118065 |            2588 |
+        """.strip()
+    )
+
+
+def test_pivot_df_single_row_two_metrics_multiindex_error():
+    """
+    Pivot table when a single column and 2 metrics are selected with multi-index,
+    designed to trigger a KeyError.
+    """
+    df = pd.DataFrame.from_dict(
+        {
+            "gender": {0: "girl", 1: "boy", 2: "girl"},
+            "age": {0: 10, 1: 12, 2: 11},
+            "SUM(num)": {0: 118065, 1: 47123, 2: 59000},
+            "MAX(num)": {0: 2588, 1: 1280, 2: 1500},
+        }
+    )
+    df.set_index(["gender", "age"], inplace=True)
+
+    df = df.drop(index="gender")
+
+    assert (
+        df.to_markdown()
+        == """
+|              |   SUM(num) |   MAX(num) |
+|:-------------|-----------:|-----------:|
+| ('girl', 10) |     118065 |       2588 |
+| ('boy', 12)  |      47123 |       1280 |
+| ('girl', 11) |      59000 |       1500 |
+        """.strip()
+    )
+
+    pivoted = pivot_df(
+        df,
+        rows=["gender", "age"],
+        columns=[],
+        metrics=["SUM(num)", "MAX(num)"],
+        aggfunc="Sum",
+        transpose_pivot=False,
+        combine_metrics=False,
+        show_rows_total=True,
+        show_columns_total=False,
+        apply_metrics_on_rows=False,
+    )
+
+    assert (
+        pivoted.to_markdown()
+        == """
+|              |   ('SUM(num)',) |   ('MAX(num)',) |   ('Total (Sum)',) |
+|:-------------|----------------:|----------------:|-------------------:|
+| ('boy', 12)  |           47123 |            1280 |              48403 |
+| ('girl', 10) |          118065 |            2588 |             120653 |
+| ('girl', 11) |           59000 |            1500 |              60500 |
+        """.strip()
+    )
+
+
 def test_pivot_df_single_row_null_values():
     """
     Pivot table when a single column and 2 metrics are selected.