posit-dev / posit-dev/py-shiny

Index issues with render.table and render.data_frame

Open
#634 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

I ran into a bit of unexpected behaviour rendering data tables. When calculating group-wise statistics in pandas we don't include the grouping column by default in the rendered table unless the user sets index=True. There are two issues with this:

  1. The default for printing pandas tables includes the index, so we should follow that pattern
  2. render.data_frame doesn't seem to have support for printing indexes, or at least follows a different API pattern
from shiny import App, render, ui, reactive
import pandas

import pandas as pd

data = {
    "group": ["A", "A", "B", "B", "A", "B", "A", "B"],
    "value1": [10, 20, 30, 40, 50, 60, 70, 80],
    "value2": [5, 15, 25, 35, 45, 55, 65, 75],
}

df = pd.DataFrame(data)

means_by_group = (
    df.set_index("group")
    .groupby(level="group")
    .agg({"value1": "mean", "value2": "mean"})
)
app_ui = ui.page_fluid(ui.output_table("table"), ui.output_data_frame("df"))


def server(input, output, session):
    @output
    @render.table(index=True)
    def table():
        return means_by_group

    @output
    @render.data_frame()
    def df():
        return means_by_group


app = App(app_ui, server)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the grouped pandas example and read the render.table(index=True) and render.data_frame entry points to compare their index APIs and defaults. Done means both renderers consistently expose the grouping/index column, with the intended default matching pandas table output.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.