Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes pandas dataframe transpose display error #613

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ repos:
hooks:
- id: ruff
stages: [commit]
args: [--fix, .]
- id: ruff-format
stages: [commit]
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
2 changes: 1 addition & 1 deletion packages/solara-widget-manager8/src
2 changes: 1 addition & 1 deletion packages/solara-widget-manager8/style
2 changes: 1 addition & 1 deletion packages/solara-widget-manager8/tsconfig.json
6 changes: 3 additions & 3 deletions solara/components/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import solara.lab
import traitlets
from solara.lab.hooks.dataframe import use_df_column_names
from solara.lab.utils.dataframe import df_len, df_records, df_slice
from solara.lab.utils.dataframe import df_len, df_records, df_slice, df_row_names

from .. import CellAction, ColumnAction

Expand Down Expand Up @@ -100,12 +100,12 @@ def DataTable(
i2 = min(total_length, (page + 1) * items_per_page)

columns = use_df_column_names(df)

rows = df_row_names(df)
items = []
dfs = df_slice(df, i1, i2)
records = df_records(dfs)
for i in range(i2 - i1):
item = {"__row__": i + i1} # special key for the row number
item = {"__row__": format(dfs, columns, i + 1, rows[i + i1])} # special key for the row number
for column in columns:
item[column] = format(dfs, column, i + i1, records[i][column])
items.append(item)
Expand Down
12 changes: 11 additions & 1 deletion solara/lab/utils/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Union


def get_pandas_major():
Expand Down Expand Up @@ -28,6 +28,16 @@ def df_columns(df) -> List[str]:
raise TypeError(f"{type(df)} not supported")


def df_row_names(df) -> List[Union[int, str]]:
"""Return a list of row names from a dataframe."""
if df_type(df) == "vaex" or df_type(df) == "polars":
return list(range(df_len(df)))
elif df_type(df) == "pandas":
return df.index.tolist()
else:
raise TypeError(f"{type(df)} not supported")


def df_slice(df, start: int, stop: int):
"""Return a subset of rows from a dataframe."""
if df_type(df) == "pandas":
Expand Down