Skip to content

fix: render data frames with non-string column names (#2115) - #2358

Open
eeshsaxena wants to merge 1 commit into
posit-dev:mainfrom
eeshsaxena:fix/data-frame-numeric-column-names
Open

fix: render data frames with non-string column names (#2115)#2358
eeshsaxena wants to merge 1 commit into
posit-dev:mainfrom
eeshsaxena:fix/data-frame-numeric-column-names

Conversation

@eeshsaxena

Copy link
Copy Markdown

Fixes #2115.

Problem

Rendering a render.DataGrid / render.data_frame from a DataFrame whose column names are non-string (e.g. numeric) fails with an obscure AttributeError:

from shiny.express import render
import pandas as pd

@render.data_frame
def table():
    df = pd.DataFrame([["a", 1], ["b", 2], ["c", 3], ["d", 4]], columns=[0, 1])
    return render.DataGrid(df)
# AttributeError: 'DataFrame' object has no attribute 'dtype'

Cause

In shiny/render/_data_frame_utils/_tbl_data.py, columns were accessed with data[col_name] on the narwhals frame:

type_hints = [serialize_dtype(data[col_name]) for col_name in data.columns]

For a numeric column name, narwhals interprets data[0] as positional/row access and returns a row frame rather than the column, so serialize_dtype receives a DataFrame instead of a Series. 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 from nw_data.columns[...] and can likewise be numeric:

nw_data[column_name].scatter(...)

Fix

Use data.get_column(col_name) / nw_data.get_column(column_name) in both places, which unambiguously accesses a column by name.

>>> serialize_frame(as_data_frame(pd.DataFrame([["a", 1], ["b", 2]], columns=[0, 1])))
# now returns columns=[0, 1], typeHints=["string", "numeric"], data=[["a", 1], ["b", 2]]

Testing

  • Added test_serialize_frame_numeric_column_names in tests/pytest/test_render_data_frame_tbl_data.py. It fails on main with the AttributeError and passes with this change.
  • Full test_render_data_frame_tbl_data.py passes (42 tests); ruff check + format clean.
  • Added a CHANGELOG entry under Bug fixes.

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Issue when rendering DataGrid with numeric column name

1 participant