Skip to content
Open
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
3 changes: 2 additions & 1 deletion superset/utils/pandas_postprocessing/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ def flatten(
df.columns = _columns

if reset_index and not isinstance(df.index, pd.RangeIndex):
df = df.reset_index(level=0)
# every level of a MultiIndex, e.g. a pivot with several index columns
df = df.reset_index()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: When a later index level shares a name with a data column or another level, reset_index() raises ValueError, failing query post-processing.

Assessment: 🟠 Major · 🔁 Occurrence: Rarely · 🏷️ Logic error

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/utils/pandas_postprocessing/flatten.py
**Line:** 105:105
**Comment:**
	*Logic Error: When a later index level shares a name with a data column or another level, `reset_index()` raises `ValueError`, failing query post-processing.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This collision isn’t new: the previous reset_index(level=0) already raises the same ValueError when level 0 shares a name with a column. This PR applies the same behaviour to every level instead of silently dropping levels 1..n. Happy to add explicit handling if maintainers prefer, but it seems out of scope for this fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Customized review instruction saved!

Instruction:

Do not flag reset_index() name-collision failures in flatten; level-0 collisions already had this behavior, and extending the reset to all levels is intentional in this change.

Applied to:

  • superset/utils/pandas_postprocessing/flatten.py

💡 To manage or update this instruction, visit: CodeAnt AI Settings

return df
16 changes: 16 additions & 0 deletions tests/unit_tests/pandas_postprocessing/test_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,19 @@ def test_escape_column_name():
"level1\\,value2" + FLAT_COLUMN_SEPARATOR + "level2\\, value2",
"level1\\,value3" + FLAT_COLUMN_SEPARATOR + "level2\\, value3",
]


def test_flat_should_reset_every_index_level():
index = pd.MultiIndex.from_arrays(
[["a", "a", "b"], [1, 2, 1]], names=["letter", "number"]
)
df = pd.DataFrame(index=index, data={"metric": [1, 2, 3]})
assert pp.flatten(df).equals(
pd.DataFrame(
{
"letter": ["a", "a", "b"],
"number": [1, 2, 1],
"metric": [1, 2, 3],
}
)
)
Loading