Skip to content

Fix KeyError when sorting melted bar chart data#13695

Merged
lukasmasuch merged 2 commits intodevelopfrom
fix/sort-bug-with-melted-bar-chart-data
Jan 26, 2026
Merged

Fix KeyError when sorting melted bar chart data#13695
lukasmasuch merged 2 commits intodevelopfrom
fix/sort-bug-with-melted-bar-chart-data

Conversation

@lukasmasuch
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch commented Jan 26, 2026

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

  • Added unit and e2e tests.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

Copilot AI review requested due to automatic review settings January 26, 2026 17:55
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 26, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13695/streamlit-1.53.1-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13695.streamlit.app (☁️ Deploy here if not accessible)

@lukasmasuch lukasmasuch added security-assessment-completed change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Jan 26, 2026
Copy link
Copy Markdown
Contributor

@sfc-gh-nbellante sfc-gh-nbellante left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_melt in built_in_chart_utils to avoid duplicate id columns when melting data, preventing pandas KeyError during chart preparation.
  • Added a Python unit test in vega_charts_test.py to verify descending sort by the x column when multiple y columns are specified, and to guard against regressions of the KeyError.
  • Extended the bar chart E2E app and Playwright tests to include a new sort scenario (x with multiple y columns) 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.

@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Jan 26, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

📉 Frontend coverage change detected

The frontend unit test (vitest) coverage has decreased by 0.0500%

  • Current PR: 86.2300% (13504 lines, 1859 missed)
  • Latest develop: 86.2800% (13504 lines, 1852 missed)

💡 Consider adding more unit tests to maintain or improve coverage.

📊 View detailed coverage comparison

@lukasmasuch lukasmasuch merged commit 5dad93c into develop Jan 26, 2026
43 checks passed
@lukasmasuch lukasmasuch deleted the fix/sort-bug-with-melted-bar-chart-data branch January 26, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants