bito-code-review[bot] commented on PR #40005:
URL: https://github.com/apache/superset/pull/40005#issuecomment-4416780756

   <!-- Bito Reply -->
   The flagged issue is correct: the function assumes MultiIndex columns are 
2-level and uses only the last level to reconstruct missing metrics, which 
fails for 3+ level MultiIndex. To resolve, modify the MultiIndex handling to 
copy full non-metric tuples from existing columns. I've implemented the concise 
fix below, preserving indentation.
   
   **superset/utils/pandas_postprocessing/pivot.py**
   ```
   if isinstance(df.columns, pd.MultiIndex):
           existing_metrics = set(df.columns.get_level_values(0))
           missing = [m for m in expected_metrics if m not in existing_metrics]
           if missing:
               non_metric_combinations = set(
                   tuple(col[1:]) for col in df.columns
               )
               for metric in missing:
                   for comb in non_metric_combinations:
                       df[(metric,) + comb] = float("nan")
   ```
   
   **tests/unit_tests/pandas_postprocessing/test_pivot.py**
   ```
   def test_pivot_preserves_all_nan_metric_multiindex():
       """
       Test restoration of all-NaN metrics in multi-level column pivots.
       """
       mock_df = DataFrame({
           "dttm": to_datetime(["2019-01-01", "2019-01-02"]),
           "col1": ["A", "B"],
           "col2": ["X", "Y"],
           "metric1": [1, 2],
           "metric2": [np.nan, np.nan],
       })
   
       df = pivot(
           df=mock_df,
           index=["dttm"],
           columns=["col1", "col2"],
           aggregates={"metric1": {"operator": "mean"}, "metric2": {"operator": 
"mean"}},
           drop_missing_columns=True,
       )
   
       # metric2 should be restored with all combinations
       assert ("metric2", "A", "X") in df.columns
       assert ("metric2", "B", "Y") in df.columns
       assert df[("metric2", "A", "X")].isna().all()
   ```


-- 
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