Fix KeyError when sorting melted bar chart data#13695
Merged
lukasmasuch merged 2 commits intodevelopfrom Jan 26, 2026
Merged
Conversation
Contributor
✅ PR preview is ready!
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a KeyError that occurred when sorting bar charts by the x column while plotting multiple y columns, and adds regression coverage for this behavior. It also updates bar chart E2E tests and snapshots to reflect the new chart case.
Changes:
- Updated
_maybe_meltinbuilt_in_chart_utilsto avoid duplicate id columns when melting data, preventing pandasKeyErrorduring chart preparation. - Added a Python unit test in
vega_charts_test.pyto verify descending sort by the x column when multiple y columns are specified, and to guard against regressions of theKeyError. - Extended the bar chart E2E app and Playwright tests to include a new sort scenario (
xwith multipleycolumns) and adjusted chart counts and indices accordingly.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/streamlit/elements/lib/built_in_chart_utils.py |
Fixes the melt logic by ensuring size_column and sort_column are not added twice to columns_to_leave_alone, eliminating duplicate id vars that caused KeyError in pandas.melt. |
lib/tests/streamlit/elements/vega_charts_test.py |
Adds a regression unit test confirming that sorting by the x column with multiple y columns no longer raises KeyError and that descending sort on the x axis is correctly encoded. |
e2e_playwright/st_bar_chart.py |
Adds a new st.bar_chart example that sorts by the x column while using multiple y columns to exercise the fixed behavior end-to-end. |
e2e_playwright/st_bar_chart_test.py |
Updates total chart count, snapshot indices, and adds a new snapshot for the multi-y x-sort case, plus shifts the add-rows chart index to remain aligned with the app. |
Contributor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Contributor
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0500%
💡 Consider adding more unit tests to maintain or improve coverage. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
Fix KeyError when using st.bar_chart() with sort parameter set to the same column as x while specifying multiple y columns.
Root cause: In _maybe_melt(), when building columns_to_leave_alone for pandas' melt operation, the code added x_column, size_column, and sort_column without checking for duplicates. When x_column == sort_column (e.g., x="a", sort="-a"), the list contained duplicates like ["a", "a"]. Pandas' melt tried to pop the column twice, succeeding the first time but failing with KeyError on the second attempt.
Fix: Added duplicate checks before appending size_column and sort_column to columns_to_leave_alone.
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.