fix: render data frames with non-string column names (#2115) - #2358
Open
eeshsaxena wants to merge 1 commit into
Open
fix: render data frames with non-string column names (#2115)#2358eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
serialize_frame and the cell-patch scatter path accessed narwhals columns via data[col_name]. For a numeric column name (e.g. 0) narwhals treats that as positional row access and returns a row frame, so serialize_dtype received a DataFrame instead of a Series and raised an obscure AttributeError. Use get_column(col_name) in both places.
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.
Fixes #2115.
Problem
Rendering a
render.DataGrid/render.data_framefrom aDataFramewhose column names are non-string (e.g. numeric) fails with an obscureAttributeError:Cause
In
shiny/render/_data_frame_utils/_tbl_data.py, columns were accessed withdata[col_name]on the narwhals frame:For a numeric column name, narwhals interprets
data[0]as positional/row access and returns a row frame rather than the column, soserialize_dtypereceives aDataFrameinstead of aSeries. Thanks to @… (the issue reporter) for the precise diagnosis.The same ambiguous access exists in the cell-patch scatter path (
apply_frame_patches), where the column name comes fromnw_data.columns[...]and can likewise be numeric:Fix
Use
data.get_column(col_name)/nw_data.get_column(column_name)in both places, which unambiguously accesses a column by name.Testing
test_serialize_frame_numeric_column_namesintests/pytest/test_render_data_frame_tbl_data.py. It fails onmainwith theAttributeErrorand passes with this change.test_render_data_frame_tbl_data.pypasses (42 tests); ruff check + format clean.An AI assistant helped implement this fix (following the root-cause and suggested approach from the issue); I reviewed the change and ran the tests locally.