Metadata-Version: 2.4
Name: cutepandas
Version: 0.6.0
Summary: Qt item models, delegates and widgets for dataframes (pandas, polars, pyarrow)
Keywords: dataframe,gui,model-view,multiindex,pandas,polars,pyarrow,pyqt6,pyside6,qt,table,widgets
Author: phil65
Author-email: phil65 <philipptemminghoff@googlemail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: User Interfaces
Classifier: Typing :: Typed
Requires-Dist: numpy>=2.5.5
Requires-Dist: pandas>=3.1.1
Requires-Dist: prettyqt>=1.61.4
Requires-Dist: pyarrow>=26.0.1
Requires-Dist: docutils>=0.23 ; extra == 'addons'
Requires-Dist: orjson>=3.13.0 ; extra == 'addons'
Requires-Dist: polars>=2.1.0 ; extra == 'polars'
Requires-Dist: pyqt6>=6.12.1 ; extra == 'pyqt6'
Requires-Dist: pyqt6-charts ; extra == 'pyqt6'
Requires-Dist: pyqt6-webengine ; extra == 'pyqt6'
Requires-Dist: pyqt6-qscintilla ; extra == 'pyqt6'
Requires-Dist: pyside6>=6.12.0 ; extra == 'pyside6'
Requires-Python: >=3.14
Project-URL: Documentation, https://phil65.github.io/cutepandas/
Project-URL: Homepage, https://github.com/phil65/cutepandas
Project-URL: Issues, https://github.com/phil65/cutepandas/issues
Project-URL: Source, https://github.com/phil65/cutepandas
Provides-Extra: addons
Provides-Extra: polars
Provides-Extra: pyqt6
Provides-Extra: pyside6
Description-Content-Type: text/markdown

# CutePandas: dataframe models and widgets for Qt

[![PyPI License](https://img.shields.io/pypi/l/cutepandas.svg)](https://pypi.org/project/cutepandas/)
[![Package status](https://img.shields.io/pypi/status/cutepandas.svg)](https://pypi.org/project/cutepandas/)
[![Python version](https://img.shields.io/pypi/pyversions/cutepandas.svg)](https://pypi.org/project/cutepandas/)
[![Build](https://github.com/phil65/cutepandas/actions/workflows/build.yml/badge.svg)](https://github.com/phil65/cutepandas/actions/workflows/build.yml)

**CutePandas** provides Qt item models, delegates and ready-made widgets for showing
dataframes in Qt applications. pandas is the first-class backend (incl. MultiIndex rows /
columns and pyarrow-backed dtypes); the table, schema and category models also show
pyarrow Tables and polars DataFrames. It is built on
[prettyqt](https://github.com/phil65/prettyqt) and works with PySide6 or PyQt6.

Documentation: https://phil65.github.io/cutepandas/

## Installation

A Qt binding is required, pick one via an extra:

```sh
pip install "cutepandas[pyside6]"
# or
pip install "cutepandas[pyqt6]"
# polars support (optional)
pip install "cutepandas[pyside6,polars]"
```

## Quickstart

```python
import pandas as pd
from prettyqt import widgets

from cutepandas import delegates, pandasmodels, pandaswidgets

app = widgets.app()
df = pd.DataFrame({"name": ["apple", "banana", "cherry"], "price": [1.2, 0.5, 3.0]})

# Composite viewer: data table plus synced row/column headers (MultiIndex aware)
viewer = pandaswidgets.DataFrameViewer(df)
viewer.show()

# Or use the models directly with any view, here with a fast string filter
model = pandasmodels.DataFrameModel(df, color_values=True)
proxy = pandasmodels.PandasStringColumnFilterProxyModel()
proxy.setSourceModel(model)
proxy.set_filter_column(0)
proxy.set_search_term("b")  # rows whose "name" starts with "b"
table = widgets.TableView()
table.set_model(proxy)
table.set_delegate(delegates.ValueColorDelegate(mode="bar"), column=1)
table.show()

app.exec()
```

## Backends: `FrameSource`

`DataFrameModel` never touches a frame directly. It reads through a `FrameSource`
(`cutepandas.frames`, Qt-free), a small protocol with adapters for pandas
(`PandasSource`), pyarrow (`ArrowSource`) and polars (`PolarsSource`, imported only when
a polars frame is passed):

| Member | Purpose |
| --- | --- |
| `backend`, `native` | backend name, the wrapped frame |
| `row_count`, `columns` | shape; `ColumnInfo(name, dtype, kind, native_dtype)` per column |
| `fetch(start, stop, columns)` | a window as Python values (`ColumnChunk`: values + null mask) |
| `fetch_arrow(start, stop, columns)` | the same window as a `pyarrow.Table` |
| `null_count`, `nbytes`, `value_range`, `value_counts`, `categories`, `describe` | column metadata and statistics |
| `sort(column, descending)` (optional, `SortableSource`) | a sorted copy (stable, nulls last) |
| `argsort(column, descending)`, `fetch_rows(rows, columns)` (optional, `PermutableSource`) | sort order as row positions; a window in any row order |
| `set_value(row, column, value)` (optional, `EditableSource`) | in-place cell edit |

`Kind` groups the backend dtypes (integer, float, string, categorical, datetime, ...) and
drives formatting, alignment and icons. Anything that implements the protocol (e.g. a
lazy source of an application) can be passed to the models instead of a frame.

```python
import polars as pl

from cutepandas import frames, pandasmodels

source = frames.as_source(pl.DataFrame({"a": [1, 2, None]}))
source.fetch(0, 2, [0])[0].values  # [1, 2]
model = pandasmodels.DataFrameModel(source)
```

### Windowed model

`DataFrameModel` fetches rows in blocks (`chunk_rows`, default 256) per column, formats
each block once with per-dtype formatters (`FormatOptions`: float precision, thousands
separator, null text, text truncation) and keeps at most `max_chunks` blocks (default
256) in an LRU `ChunkCache`. Only the visible window is ever converted to Python
objects, so memory is the frame plus a small, bounded cache. Background colours
(`color_values=True`) and normalised values (`NormalizedRole`, used by
`ValueColorDelegate`) are computed per block with NumPy, not per `data()` call.

Measured on a 10M-row x 10-column frame (4 float, 2 int, datetime, categorical,
unique strings, bool; about 700 MB), four roles of every cell of a 40 x 10 viewport at
random positions, cold cache: median 4.5 ms, p95 8 ms, max 11 ms for pandas, pyarrow
and polars alike. The full cache (256 blocks) adds about 9 MB. With `color_values`, the
first paint scans each numeric column once for its range (0.04-0.4 s on 10M rows).

### Sorting

A header click in `DataFrameViewer` emits `sort_requested(column, order)` (a
`Qt.SortOrder`; the same column again reverses). What happens next:

- `sortable=False`: nothing else. The application sorts under its own control (e.g.
  within a memory budget, or as a step of a pipeline), shows the result with
  `set_df()` and restores the indicator with `set_sort_state(column, order)`.
- `sortable=True` (default) and `sort_mode="copy"` (default): the source builds a sorted
  copy (`SortableSource.sort`) that replaces the frame, as large as the frame itself.
- `sortable=True` and `sort_mode="permutation"`: a display-only sort. The source
  computes the order as row positions (`PermutableSource.argsort`; 8 bytes per row),
  stored as `DataFrameHolder.row_order`; the frame is neither copied nor changed and
  every visible block is read through the order (`fetch_rows`). The order is computed
  in a worker thread (`sorting_changed(True / False)`); only the latest request is
  applied, only if the frame was not replaced or edited meanwhile, and
  `cancel_sort()` drops a running one. Replacing the frame or `clear_sort()` restores
  the frame's order; row labels follow the order (MultiIndex spans are off while
  sorted).

The building blocks are public: `DataFrameModel.compute_sort_permutation(column,
order)` (any thread) returns a `SortPermutation`, `apply_sort_permutation()` (GUI
thread) applies it or returns False if it is stale, `source_row(row)` maps a model row
to a frame row. Sort orders are stable with nulls last; pandas uses NumPy's stable
argsort for NumPy dtypes and Arrow's sort kernel for Arrow-backed columns (incl. the
pandas 3 `str` dtype), pyarrow Arrow's sort kernel, polars its own (multi-threaded)
sort.

Measured on 10M rows (one column sorted, descending): computing the order takes
1.6 s (int) / 2.1 s (float) / 6.1 s (str) for pandas, 1.8 / 2.2 / 4.9 s for pyarrow and
0.35 / 0.33 / 1.3 s for polars; the order takes 80 MB. A cold 60 x 3 viewport read
through it: median 0.8-1.2 ms, max 3.3 ms on every backend.

### Translations

cutepandas ships English labels only. `SchemaModel`, `PandasColumnListModel`,
`PandasIndexListModel`, `CategoryModel`, `TopValuesModel`, `ColumnProfileWidget` and
`DataFrameWidget` take `translate=` (a `str -> str` callable such as a gettext
function) and look each label up when they show it; `set_translate()` swaps it and
`retranslate()` re-shows the labels after a language switch. The English labels are
marked with `cutepandas.utils.i18n.N_`, so `xgettext --keyword=N_` (or babel
`-k N_`) extracts them for a catalog.

### Threads

- GUI thread only: every Qt method of the models (`data`, `setData`, `sort`, assigning
  `df`, the setters), `DataFrameHolder`, all widgets, `ColumnProfileWidget.set_stats`.
- Any thread: `DataFrameModel.compute_sort_permutation`,
  `DataFrameModel.prefetch(first_row, last_row, columns)` (fills the cache,
  e.g. ahead of scrolling), the `ChunkCache` (all mutations under a lock; `clear()` /
  `invalidate()` start a new generation so a block built from old data is dropped) and
  the read methods of a `FrameSource` (`fetch`, `describe`, ...; e.g. compute column
  statistics in a worker and hand them to `ColumnProfileWidget.set_stats`).
- `set_value` edits the frame in place: don't run it while a worker reads the frame.

There is no module-level mutable state.

## Features

- **Models**: `DataFrameModel` (any backend; MultiIndex levels joined by a configurable
  separator), `VerticalHeaderModel` / `HorizontalHeaderModel` (one cell per index level),
  `SchemaModel` (columns with dtype, icon, null count, size) and `CategoryModel`
  (categories with code and count; pandas `category`, polars `Enum` / `Categorical`,
  Arrow dictionaries). The pandas models `PandasColumnListModel`,
  `PandasCategoryListModel` and `PandasIndexListModel` also edit the frame.
- **Filter proxies** (pandas) that filter with NumPy boolean indexes instead of
  per-cell Python calls, which is much faster than `QSortFilterProxyModel` on large
  frames: `PandasStringColumnFilterProxyModel`,
  `PandasMultiStringColumnFilterProxyModel`, `PandasEvalFilterProxyModel` (filter with
  a `DataFrame.eval` expression).
- **Delegates**: `DTypeDelegate` (pick a dtype from a combo box with dtype icons),
  `ValueColorDelegate` (data bar or heat map from `NormalizedRole`).
- **Widgets**: `DataFrameViewer` (data and header tables kept in sync, spans for
  MultiIndex), `ColumnProfileWidget` (dtype, counts, min / max / mean / std, top values
  with share bars), `DataFrameWidget` (viewer with docks for index, columns,
  categories, column profile and `df.attrs`), `DataFrameListWidget` and
  `DataFrameManagerWidget` for several DataFrames.
- **Icons**: `cutepandas.utils.icons.icon_for_dtype` / `icon_for_kind` for pandas,
  pyarrow and polars dtypes.

## Development

```sh
uv sync --all-extras
uv run pytest
uv run ruff check . && uv run ty check
```

## License

[MIT](https://github.com/phil65/cutepandas/blob/main/LICENSE)
