FrancescoCastaldi commented on issue #45261:
URL: https://github.com/apache/superset/issues/45261#issuecomment-6524025516
### Root Cause Analysis & Solution
In `superset/utils/pandas_postprocessing/flatten.py`, the index reset was
implemented as:
```python
if reset_index and not isinstance(df.index, pd.RangeIndex):
df = df.reset_index(level=0)
```
When a pivot operation uses multiple index columns (e.g. pivoting on two or
more row dimensions), the result has a multi-level index. Calling
`reset_index(level=0)` only converts the first level into a regular column,
leaving all subsequent levels trapped in the index. When serialized to records
or JSON, the untransformed index levels are lost.
#### Solution
Remove `level=0` to reset all index levels into columns:
```python
if reset_index and not isinstance(df.index, pd.RangeIndex):
df = df.reset_index()
```
Verified with unit tests in
`tests/unit_tests/pandas_postprocessing/test_flatten.py` and committed to
branch `fix/community-unanswered-issues` (commit `2de5ed61ce1`) at
https://github.com/FrancescoCastaldi/superset/tree/fix/community-unanswered-issues.
--
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]