dosubot[bot] commented on issue #40709: URL: https://github.com/apache/superset/issues/40709#issuecomment-4612360696
<!-- Answer --> Thanks for the detailed report! I can confirm this is a bug in the pandas postprocessing `rank` function. The issue is in [`superset/utils/pandas_postprocessing/rank.py` at line 37](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/pandas_postprocessing/rank.py#L35-L37): ```python if group_by: gb = df.groupby(group_by, group_keys=False) df["rank"] = gb.apply(lambda x: x[metric].rank(pct=True)) ``` When "Normalize Across X" is selected and a filter reduces the data to a single X value (single column), the `groupby` on that column produces groups where each group has only one row. In newer versions of pandas, `gb.apply(lambda x: x[metric].rank(pct=True))` can return a DataFrame with multiple columns instead of a Series when each group has a single element, causing the `ValueError: Cannot set a DataFrame with multiple columns to the single column rank` error. This is a known pandas behavior change — when `apply` is called on single-element groups, pandas may not properly infer the return type and returns a DataFrame instead of a Series. **A potential fix** would be to ensure the result is always a Series before assignment, for example: ```python if group_by: gb = df.groupby(group_by, group_keys=False) result = gb.apply(lambda x: x[metric].rank(pct=True)) if isinstance(result, pd.DataFrame): result = result.iloc[:, 0] df["rank"] = result else: df["rank"] = df[metric].rank(pct=True) ``` Or alternatively, using `transform` instead of `apply` which always returns a Series aligned with the original DataFrame: ```python if group_by: gb = df.groupby(group_by, group_keys=False) df["rank"] = gb[metric].transform(lambda x: x.rank(pct=True)) ``` The `transform` approach is likely cleaner since it avoids the `apply` return-type ambiguity altogether and is semantically more appropriate here (we want a value per row, not an aggregation). For the secondary issue (empty plot when both X and Y are reduced to a single value with normalize across X/Y), that's likely because a single-element group produces a rank of `NaN` or `1.0` that may not map correctly to the visualization — but fixing the `ValueError` above should make that case reachable for further debugging. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).* --- Share context across your team and agents. Try [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-tagline&utm_term=apache%2Fsuperset). [](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-feedback&utm_term=apache%2Fsuperset&message_id=ad960abe-149e-4b02-a989-816cbb1510b2) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-learn-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-add-team&utm_term=apache%2Fsuperset) -- 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]
