mistercrunch commented on code in PR #37869:
URL: https://github.com/apache/superset/pull/37869#discussion_r2791188202


##########
superset/viz.py:
##########
@@ -747,6 +747,8 @@ def get_data(self, df: pd.DataFrame) -> VizData:
         pt = df.pivot_table(index=DTTM_ALIAS, columns=columns, values=values)
         pt.index = pt.index.map(str)
         pt = pt.sort_index()
+        if isinstance(pt.columns, pd.MultiIndex):
+            pt.columns = [", ".join(str(s) for s in col) for col in pt.columns]

Review Comment:
   Good catch — updated to use `escape_separator` and `FLAT_COLUMN_SEPARATOR` 
from `superset.utils.pandas_postprocessing.utils` for consistency and to handle 
commas in group-by values.



##########
tests/integration_tests/viz_tests.py:
##########
@@ -626,6 +626,39 @@ def test_get_data_group_by(self):
         }
         assert expected == data["records"]
 
+    def test_get_data_multiple_group_by(self):
+        form_data = {"metrics": ["sum__A"], "groupby": ["groupby1", 
"groupby2"]}
+        datasource = self.get_datasource_mock()
+        raw = {}
+        t1 = pd.Timestamp("2000")
+        t2 = pd.Timestamp("2002")
+        raw[DTTM_ALIAS] = [t1, t1, t1, t1, t2, t2, t2, t2]
+        raw["sum__A"] = [15, 20, 25, 30, 35, 40, 45, 50]
+        raw["groupby1"] = ["a1", "a2", "a1", "a2", "a1", "a2", "a1", "a2"]
+        raw["groupby2"] = ["b1", "b1", "b2", "b2", "b1", "b1", "b2", "b2"]
+        df = pd.DataFrame(raw)
+        test_viz = viz.TimeTableViz(datasource, form_data)
+        data = test_viz.get_data(df)
+        # Columns should be flattened strings, not tuples
+        assert {"a1, b1", "a1, b2", "a2, b1", "a2, b2"} == set(data["columns"])
+        time_format = "%Y-%m-%d %H:%M:%S"
+        expected = {
+            t1.strftime(time_format): {
+                "a1, b1": 15,
+                "a1, b2": 25,
+                "a2, b1": 20,
+                "a2, b2": 30,
+            },
+            t2.strftime(time_format): {
+                "a1, b1": 35,
+                "a1, b2": 45,
+                "a2, b1": 40,
+                "a2, b2": 50,

Review Comment:
   Updated — the test now uses `FLAT_COLUMN_SEPARATOR` via a `sep` variable 
throughout.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to