Skip to content

DataFrameModel

Base classes

Name Children Inherits
DataFrameHolderMixin
cutepandas.pandasmodels.dataframeholder
Model mixin: keeps `self.df` in a (possibly shared) DataFrameHolder.
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

⋔ Inheritance diagram

graph TD
  841442073["dataframemodel.DataFrameModel"]
  103874424["dataframeholder.DataFrameHolderMixin"]
  2751680898["builtins.object"]
  656210031["core.AbstractTableModel"]
  4092674192["core.AbstractTableModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  1115338939["QtCore.QAbstractTableModel"]
  930985301["QtCore.QAbstractItemModel"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  103874424 --> 841442073
  2751680898 --> 103874424
  656210031 --> 841442073
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  1115338939 --> 656210031
  930985301 --> 1115338939
  13951691 --> 930985301
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: DataFrameHolderMixin, AbstractTableModel

Virtual table model for pandas, pyarrow and polars frames.

The model reads through a FrameSource and converts only what the view asks for: rows are fetched in blocks of chunk_rows per column, formatted once (per-dtype formatters, see FormatOptions) and kept in a bounded LRU ChunkCache. A 10M-row frame therefore costs the frame itself plus at most max_chunks converted blocks.

With color_values=True, numeric cells get a background whose intensity follows the value within the column's (min, max); booleans are green / red. The colours are computed per block (vectorised), not per data() call.

Sorting (sort, e.g. from a header click) has two modes:

  • "copy" (default): the source builds a sorted copy of the frame (SortableSource.sort), which replaces the holder's frame. The copy is as large as the frame and the old frame stays alive as long as someone references it.
  • "permutation": display-only. The source returns the sort order as row positions (PermutableSource.argsort, 8 bytes per row), stored as the holder's row_order; the frame is not copied or changed, and every visible block is read through the order (PermutableSource.fetch_rows). Row i of the model is row source_row(i) of the frame. Replacing the frame or clear_sort() drops the order.

Threads: all Qt methods (data, setData, sort, assigning df, setters) run on the GUI thread. prefetch() may run on any thread; it only fills the cache.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
class DataFrameModel(DataFrameHolderMixin, core.AbstractTableModel):
    """Virtual table model for pandas, pyarrow and polars frames.

    The model reads through a `FrameSource` and converts only what the view asks for:
    rows are fetched in blocks of `chunk_rows` per column, formatted once (per-dtype
    formatters, see `FormatOptions`) and kept in a bounded LRU `ChunkCache`. A 10M-row
    frame therefore costs the frame itself plus at most `max_chunks` converted blocks.

    With `color_values=True`, numeric cells get a background whose intensity follows
    the value within the column's (min, max); booleans are green / red. The colours
    are computed per block (vectorised), not per `data()` call.

    Sorting (`sort`, e.g. from a header click) has two modes:

    - `"copy"` (default): the source builds a sorted copy of the frame
      (`SortableSource.sort`), which replaces the holder's frame. The copy is as large
      as the frame and the old frame stays alive as long as someone references it.
    - `"permutation"`: display-only. The source returns the sort order as row
      positions (`PermutableSource.argsort`, 8 bytes per row), stored as the holder's
      `row_order`; the frame is not copied or changed, and every visible block is
      read through the order (`PermutableSource.fetch_rows`). Row `i` of the model is
      row `source_row(i)` of the frame. Replacing the frame or `clear_sort()` drops
      the order.

    Threads: all Qt methods (`data`, `setData`, `sort`, assigning `df`, setters) run
    on the GUI thread. `prefetch()` may run on any thread; it only fills the cache.
    """

    SUPPORTS = pd.DataFrame

    class Roles(enum.IntEnum):
        """Role names."""

        DTypeRole = constants.USER_ROLE + 4555
        """Backend dtype object of the column."""
        ColumnNameRole = constants.USER_ROLE + 4556
        """Column label."""
        KindRole = constants.USER_ROLE + 4557
        """`Kind` of the column."""
        IsNullRole = constants.USER_ROLE + 4558
        """True if the cell is missing."""
        NormalizedRole = constants.USER_ROLE + 4559
        """Numeric cell value scaled to 0..1 within the column (None if not numeric)."""

    def __init__(
        self,
        df: Any = None,
        *args,
        chunk_rows: int = 256,
        max_chunks: int = 256,
        format_options: FormatOptions | None = None,
        color_values: bool = False,
        dtype_icons: bool = False,
        read_only: bool = False,
        sort_mode: SortMode = "copy",
        **kwargs,
    ):
        """Show a frame.

        Args:
            df: pandas DataFrame / Series, pyarrow Table, polars DataFrame, a
                `FrameSource` or a `DataFrameHolder` (to share the frame with
                other models).
            chunk_rows: Rows per cached block.
            max_chunks: Maximum number of cached blocks (all columns together).
            format_options: How values are formatted for display.
            color_values: Colour numeric / boolean cells by value.
            dtype_icons: Show a dtype icon in the column headers.
            read_only: Disable editing even if the backend supports it.
            sort_mode: `"copy"` (sorted copy of the frame) or `"permutation"`
                (display-only row order, no copy); see the class docstring.
            args: Passed on to the Qt base class.
            kwargs: Passed on to the Qt base class.
        """
        self._multiindex_separator = " | "
        self._options = format_options or FormatOptions()
        self._color_values = color_values
        self._dtype_icons = dtype_icons
        self._kind_icons = icons.KindIconCache()
        self._read_only = read_only
        self._sort_mode: SortMode = sort_mode
        self._data_generation = 0
        self._palette: list[Any] | None = None
        self._cache: ChunkCache[CellChunk] = ChunkCache(chunk_rows, max_chunks)
        self._chunk_rows = chunk_rows
        self._state = _make_state(None, self._options)
        super().__init__(*args, **kwargs)
        self._init_holder(df)
        self.holder.row_order_about_to_change.connect(self._on_row_order_about_to_change)
        self.holder.row_order_changed.connect(self._on_row_order_changed)
        self._refresh_state()

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, pd.DataFrame | pa.Table) or is_polars_frame(instance)

    # --- frame state ---

    def _refresh_state(self):
        self._cache.clear()
        self._state = _make_state(self.source, self._options, self.holder.row_order)

    def _on_holder_changed(self, origin: Any):
        # Also for changes made by this model (sort): the cache belongs to the old frame.
        self._data_generation += 1
        self._refresh_state()
        super()._on_holder_changed(origin)

    def _on_row_order_about_to_change(self, origin: Any):
        self.layoutAboutToBeChanged.emit()

    def _on_row_order_changed(self, origin: Any):
        self._refresh_state()  # blocks were read in the old order
        self.layoutChanged.emit()

    @property
    def cache(self) -> ChunkCache[CellChunk]:
        return self._cache

    @property
    def column_info(self) -> Sequence[ColumnInfo]:
        return self._state.columns

    # --- chunk building (thread-safe) ---

    def _column_range(self, state: _FrameState, col: int) -> tuple[float, float] | None:
        with state.lock:
            if col in state.ranges:
                return state.ranges[col]
        assert state.source is not None
        value_range = state.source.value_range(col)  # full scan, outside the lock
        with state.lock:
            state.ranges[col] = value_range
        return value_range

    def _get_palette(self) -> list[Any]:
        if self._palette is None:
            color = gui.QColor("#e4572e")
            self._palette = [
                gui.QColor(
                    color.red(),
                    color.green(),
                    color.blue(),
                    int(i * 200 / (_PALETTE_STEPS - 1)),
                )
                for i in range(_PALETTE_STEPS)
            ]
        return self._palette

    def _add_colors(self, state: _FrameState, col: int, chunk: CellChunk):
        chunk.scaled = True
        kind = state.columns[col].kind
        if kind == Kind.BOOLEAN:
            true, false = gui.QColor(0, 160, 0, 50), gui.QColor(220, 0, 0, 50)
            chunk.normalized = [None if v is None else float(v) for v in chunk.values]
            chunk.colors = [
                None if v is None else (true if v else false) for v in chunk.values
            ]
            return
        if kind not in {Kind.INTEGER, Kind.FLOAT, Kind.DECIMAL}:
            return
        value_range = self._column_range(state, col)
        if value_range is None:
            return
        lo, hi = value_range
        try:
            values = np.array(chunk.values, dtype=float)
        except TypeError, ValueError:  # complex numbers
            return
        span = hi - lo
        scaled = (values - lo) / span if span else np.zeros_like(values)
        valid = np.isfinite(scaled)
        scaled = np.where(valid, np.clip(scaled, 0.0, 1.0), 0.0)
        steps = (scaled * (_PALETTE_STEPS - 1)).astype(np.intp)
        palette = self._get_palette()
        chunk.normalized = [
            s if ok else None for s, ok in zip(scaled.tolist(), valid.tolist())
        ]
        chunk.colors = [
            palette[s] if ok else None for s, ok in zip(steps.tolist(), valid.tolist())
        ]

    def _build_chunk(self, block: int, col: int) -> CellChunk:
        state = self._state
        assert state.source is not None
        start = block * self._chunk_rows
        stop = min(start + self._chunk_rows, state.row_count)
        if state.row_order is None:
            [fetched] = state.source.fetch(start, stop, [col])
        else:
            assert isinstance(state.source, PermutableSource)
            [fetched] = state.source.fetch_rows(state.row_order[start:stop], [col])
        display = format_chunk(
            fetched.values, fetched.nulls, state.formatters[col], self._options.null_text
        )
        chunk = CellChunk(start, fetched.values, fetched.nulls.tolist(), display)
        if self._color_values:
            self._add_colors(state, col, chunk)
        return chunk

    def _chunk(self, row: int, col: int) -> CellChunk:
        block = row // self._chunk_rows
        chunk = self._cache.get(block, col)
        if chunk is None:
            chunk = self._cache.get_or_build(block, col, self._build_chunk)
        return chunk

    def _scaled_chunk(self, row: int, col: int) -> CellChunk:
        chunk = self._chunk(row, col)
        if not chunk.scaled:  # on demand, once per block (GUI thread)
            self._add_colors(self._state, col, chunk)
        return chunk

    def source_row(self, row: int) -> int:
        """Row position in the frame of the model's row `row` (differs once sorted)."""
        order = self._state.row_order
        return row if order is None else int(order[row])

    def fetch_window(
        self, start: int, stop: int, columns: Sequence[int]
    ) -> list[ColumnChunk]:
        """Model rows `start:stop` of the given columns, in display order."""
        state = self._state
        if state.source is None:
            return []
        if state.row_order is None:
            return state.source.fetch(start, stop, columns)
        assert isinstance(state.source, PermutableSource)
        return state.source.fetch_rows(state.row_order[start:stop], columns)

    def prefetch(
        self, first_row: int, last_row: int, columns: Sequence[int] | None = None
    ):
        """Convert the blocks covering rows `first_row..last_row` (any thread)."""
        state = self._state
        if state.source is None or state.row_count == 0:
            return
        last_row = min(last_row, state.row_count - 1)
        cols = range(len(state.columns)) if columns is None else columns
        for block in range(
            first_row // self._chunk_rows, last_row // self._chunk_rows + 1
        ):
            for col in cols:
                self._cache.get_or_build(block, col, self._build_chunk)

    # --- Qt model API ---

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        if parent is not None and parent.isValid():
            return 0
        return self._state.row_count

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        if parent is not None and parent.isValid():
            return 0
        return len(self._state.columns)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid() or self._state.source is None:
            return None
        row, col = index.row(), index.column()
        match role:
            case constants.DISPLAY_ROLE:
                chunk = self._chunk(row, col)
                return chunk.display[row - chunk.start]
            case constants.ALIGNMENT_ROLE:
                return self._state.alignments[col]
            case constants.BACKGROUND_ROLE if self._color_values:
                chunk = self._scaled_chunk(row, col)
                return None if chunk.colors is None else chunk.colors[row - chunk.start]
            case constants.EDIT_ROLE | constants.USER_ROLE:
                chunk = self._chunk(row, col)
                return chunk.values[row - chunk.start]
            case constants.TOOLTIP_ROLE:
                chunk = self._chunk(row, col)
                offset = row - chunk.start
                return "null" if chunk.nulls[offset] else str(chunk.values[offset])
            case self.Roles.IsNullRole:
                chunk = self._chunk(row, col)
                return chunk.nulls[row - chunk.start]
            case self.Roles.NormalizedRole:
                chunk = self._scaled_chunk(row, col)
                if chunk.normalized is None:
                    return None
                return chunk.normalized[row - chunk.start]
            case self.Roles.DTypeRole:
                return self._state.columns[col].native_dtype
            case self.Roles.KindRole:
                return self._state.columns[col].kind
            case self.Roles.ColumnNameRole:
                return self._state.columns[col].name
        return None

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        columns = self._state.columns
        if orientation == constants.VERTICAL:
            if not 0 <= section < self._state.row_count:
                return None
            if role in {constants.DISPLAY_ROLE, constants.TOOLTIP_ROLE}:
                row = self.source_row(section)
                if isinstance(self.df, pd.DataFrame):
                    return format_name(self.df.index[row], self._multiindex_separator)
                return str(row)
            if role == constants.ALIGNMENT_ROLE:
                return constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
            return None
        if not 0 <= section < len(columns):
            return None
        info = columns[section]
        match role:
            case constants.DISPLAY_ROLE:
                return format_name(info.name, self._multiindex_separator)
            case constants.TOOLTIP_ROLE:
                label = format_name(info.name, self._multiindex_separator)
                return f"{label}\n{info.dtype}"
            case constants.ALIGNMENT_ROLE:
                return constants.ALIGN_CENTER
            case constants.DECORATION_ROLE if self._dtype_icons:
                return self._kind_icons.icon(info.kind)
            case self.Roles.DTypeRole:
                return info.native_dtype
            case self.Roles.KindRole:
                return info.kind
            case self.Roles.ColumnNameRole:
                return info.name
        return None

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        if not index.isValid():
            return constants.NO_FLAGS
        return _EDITABLE_FLAGS if self.is_editable() else _READ_ONLY_FLAGS

    def is_editable(self) -> bool:
        return not self._read_only and isinstance(self._state.source, EditableSource)

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        source = self._state.source
        if not index.isValid() or not self.is_editable():
            return False
        if role not in {constants.EDIT_ROLE, constants.USER_ROLE}:
            return False
        assert isinstance(source, EditableSource)
        row, col = index.row(), index.column()
        try:
            # In place: every model sharing the holder sees the same object.
            source.set_value(self.source_row(row), col, value)
        except Exception:
            logger.exception("Could not set %r at %s", value, (row, col))
            return False
        self._data_generation += 1  # a sort order computed before is stale now
        state = self._state
        with state.lock:
            state.ranges.pop(col, None)
        self._cache.invalidate(column=col)
        if self._color_values:  # the column range may have changed
            last = self.index(state.row_count - 1, col)
            self.dataChanged.emit(self.index(0, col), last)
        else:
            self.dataChanged.emit(index, index)
        return True

    def sort(self, ncol: int, order: constants.SortOrder = constants.ASCENDING):
        """Sort by column `ncol` (see `sort_mode`); unsupported sources are ignored."""
        source = self._state.source
        if not 0 <= ncol < self.columnCount():
            return
        descending = order == constants.DESCENDING
        if self._sort_mode == "permutation":
            if (permutation := self.compute_sort_permutation(ncol, order)) is not None:
                self.apply_sort_permutation(permutation)
            return
        if not isinstance(source, SortableSource):
            return
        new = source.sort(ncol, descending=descending)
        with self.change_layout():
            self.df = new

    def compute_sort_permutation(
        self, column: int, order: constants.SortOrder = constants.ASCENDING
    ) -> SortPermutation | None:
        """Compute a display-only sort order (any thread, e.g. a worker).

        Reads the frame through `PermutableSource.argsort` (stable, nulls last);
        the result is one integer per row. Returns None if the source can't sort.
        Apply it with `apply_sort_permutation` on the GUI thread.
        """
        generation = self._data_generation  # before reading: edits make it stale
        source = self._state.source
        if not isinstance(source, PermutableSource):
            return None
        descending = order == constants.DESCENDING
        positions = source.argsort(column, descending=descending)
        return SortPermutation(column, descending, positions, generation)

    def apply_sort_permutation(self, permutation: SortPermutation) -> bool:
        """Show the rows in a computed order (GUI thread).

        Returns False (and changes nothing) if the frame was replaced or edited
        since the order was computed.
        """
        if permutation.generation != self._data_generation:
            return False
        self.holder.set_row_order(permutation.positions, origin=self)
        return True

    def can_sort(self) -> bool:
        """True if the source supports sorting in the current `sort_mode`."""
        protocol = (
            PermutableSource if self._sort_mode == "permutation" else SortableSource
        )
        return isinstance(self._state.source, protocol)

    def clear_sort(self):
        """Back to the frame's own row order (drops a display-only sort)."""
        if self.holder.row_order is not None:
            self.holder.set_row_order(None, origin=self)

    @property
    def row_order(self) -> np.ndarray | None:
        """Display order of the rows (row positions), None if not permuted."""
        return self._state.row_order

    # --- options ---

    def _emit_all_changed(self):
        rows, cols = self.rowCount(), self.columnCount()
        if rows and cols:
            self.dataChanged.emit(self.index(0, 0), self.index(rows - 1, cols - 1))

    def set_format_options(self, options: FormatOptions):
        self._options = options
        self._refresh_state()
        self._emit_all_changed()

    def get_format_options(self) -> FormatOptions:
        return self._options

    def set_color_values(self, value: bool):
        self._color_values = value
        self._cache.clear()
        self._emit_all_changed()

    def get_color_values(self) -> bool:
        return self._color_values

    def set_dtype_icons(self, value: bool):
        self._dtype_icons = value
        if self.columnCount():
            self.headerDataChanged.emit(constants.HORIZONTAL, 0, self.columnCount() - 1)

    def get_dtype_icons(self) -> bool:
        return self._dtype_icons

    def set_sort_mode(self, mode: SortMode):
        if mode not in {"copy", "permutation"}:
            msg = f"Invalid sort mode {mode!r}"
            raise ValueError(msg)
        self._sort_mode = mode

    def get_sort_mode(self) -> SortMode:
        return self._sort_mode

    def set_read_only(self, value: bool):
        self._read_only = value

    def is_read_only(self) -> bool:
        return self._read_only

    def set_multiindex_separator(self, separator: str):
        self._multiindex_separator = separator

    def get_multiindex_separator(self) -> str:
        return self._multiindex_separator

    multiindex_separator = core.Property(
        str,
        get_multiindex_separator,
        set_multiindex_separator,
        doc="Separator to use for MultiIndexes",
    )
    color_values = core.Property(
        bool,
        get_color_values,
        set_color_values,
        doc="Colour numeric / boolean cells by value",
    )

row_order property

row_order: ndarray | None

Display order of the rows (row positions), None if not permuted.

Roles

Bases: IntEnum

Role names.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
class Roles(enum.IntEnum):
    """Role names."""

    DTypeRole = constants.USER_ROLE + 4555
    """Backend dtype object of the column."""
    ColumnNameRole = constants.USER_ROLE + 4556
    """Column label."""
    KindRole = constants.USER_ROLE + 4557
    """`Kind` of the column."""
    IsNullRole = constants.USER_ROLE + 4558
    """True if the cell is missing."""
    NormalizedRole = constants.USER_ROLE + 4559
    """Numeric cell value scaled to 0..1 within the column (None if not numeric)."""

ColumnNameRole class-attribute instance-attribute

ColumnNameRole = constants.USER_ROLE + 4556

Column label.

DTypeRole class-attribute instance-attribute

DTypeRole = constants.USER_ROLE + 4555

Backend dtype object of the column.

IsNullRole class-attribute instance-attribute

IsNullRole = constants.USER_ROLE + 4558

True if the cell is missing.

KindRole class-attribute instance-attribute

KindRole = constants.USER_ROLE + 4557

Kind of the column.

NormalizedRole class-attribute instance-attribute

NormalizedRole = constants.USER_ROLE + 4559

Numeric cell value scaled to 0..1 within the column (None if not numeric).

__init__

__init__(
    df: Any = None,
    *args,
    chunk_rows: int = 256,
    max_chunks: int = 256,
    format_options: FormatOptions | None = None,
    color_values: bool = False,
    dtype_icons: bool = False,
    read_only: bool = False,
    sort_mode: SortMode = "copy",
    **kwargs,
)

Show a frame.

Parameters:

Name Type Description Default
df Any

pandas DataFrame / Series, pyarrow Table, polars DataFrame, a FrameSource or a DataFrameHolder (to share the frame with other models).

None
chunk_rows int

Rows per cached block.

256
max_chunks int

Maximum number of cached blocks (all columns together).

256
format_options FormatOptions | None

How values are formatted for display.

None
color_values bool

Colour numeric / boolean cells by value.

False
dtype_icons bool

Show a dtype icon in the column headers.

False
read_only bool

Disable editing even if the backend supports it.

False
sort_mode SortMode

"copy" (sorted copy of the frame) or "permutation" (display-only row order, no copy); see the class docstring.

'copy'
args

Passed on to the Qt base class.

()
kwargs

Passed on to the Qt base class.

{}
Source code in src/cutepandas/pandasmodels/dataframemodel.py
def __init__(
    self,
    df: Any = None,
    *args,
    chunk_rows: int = 256,
    max_chunks: int = 256,
    format_options: FormatOptions | None = None,
    color_values: bool = False,
    dtype_icons: bool = False,
    read_only: bool = False,
    sort_mode: SortMode = "copy",
    **kwargs,
):
    """Show a frame.

    Args:
        df: pandas DataFrame / Series, pyarrow Table, polars DataFrame, a
            `FrameSource` or a `DataFrameHolder` (to share the frame with
            other models).
        chunk_rows: Rows per cached block.
        max_chunks: Maximum number of cached blocks (all columns together).
        format_options: How values are formatted for display.
        color_values: Colour numeric / boolean cells by value.
        dtype_icons: Show a dtype icon in the column headers.
        read_only: Disable editing even if the backend supports it.
        sort_mode: `"copy"` (sorted copy of the frame) or `"permutation"`
            (display-only row order, no copy); see the class docstring.
        args: Passed on to the Qt base class.
        kwargs: Passed on to the Qt base class.
    """
    self._multiindex_separator = " | "
    self._options = format_options or FormatOptions()
    self._color_values = color_values
    self._dtype_icons = dtype_icons
    self._kind_icons = icons.KindIconCache()
    self._read_only = read_only
    self._sort_mode: SortMode = sort_mode
    self._data_generation = 0
    self._palette: list[Any] | None = None
    self._cache: ChunkCache[CellChunk] = ChunkCache(chunk_rows, max_chunks)
    self._chunk_rows = chunk_rows
    self._state = _make_state(None, self._options)
    super().__init__(*args, **kwargs)
    self._init_holder(df)
    self.holder.row_order_about_to_change.connect(self._on_row_order_about_to_change)
    self.holder.row_order_changed.connect(self._on_row_order_changed)
    self._refresh_state()

apply_sort_permutation

apply_sort_permutation(permutation: SortPermutation) -> bool

Show the rows in a computed order (GUI thread).

Returns False (and changes nothing) if the frame was replaced or edited since the order was computed.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def apply_sort_permutation(self, permutation: SortPermutation) -> bool:
    """Show the rows in a computed order (GUI thread).

    Returns False (and changes nothing) if the frame was replaced or edited
    since the order was computed.
    """
    if permutation.generation != self._data_generation:
        return False
    self.holder.set_row_order(permutation.positions, origin=self)
    return True

can_sort

can_sort() -> bool

True if the source supports sorting in the current sort_mode.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def can_sort(self) -> bool:
    """True if the source supports sorting in the current `sort_mode`."""
    protocol = (
        PermutableSource if self._sort_mode == "permutation" else SortableSource
    )
    return isinstance(self._state.source, protocol)

clear_sort

clear_sort()

Back to the frame's own row order (drops a display-only sort).

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def clear_sort(self):
    """Back to the frame's own row order (drops a display-only sort)."""
    if self.holder.row_order is not None:
        self.holder.set_row_order(None, origin=self)

compute_sort_permutation

compute_sort_permutation(column: int, order: SortOrder = ASCENDING) -> SortPermutation | None

Compute a display-only sort order (any thread, e.g. a worker).

Reads the frame through PermutableSource.argsort (stable, nulls last); the result is one integer per row. Returns None if the source can't sort. Apply it with apply_sort_permutation on the GUI thread.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def compute_sort_permutation(
    self, column: int, order: constants.SortOrder = constants.ASCENDING
) -> SortPermutation | None:
    """Compute a display-only sort order (any thread, e.g. a worker).

    Reads the frame through `PermutableSource.argsort` (stable, nulls last);
    the result is one integer per row. Returns None if the source can't sort.
    Apply it with `apply_sort_permutation` on the GUI thread.
    """
    generation = self._data_generation  # before reading: edits make it stale
    source = self._state.source
    if not isinstance(source, PermutableSource):
        return None
    descending = order == constants.DESCENDING
    positions = source.argsort(column, descending=descending)
    return SortPermutation(column, descending, positions, generation)

fetch_window

fetch_window(start: int, stop: int, columns: Sequence[int]) -> list[ColumnChunk]

Model rows start:stop of the given columns, in display order.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def fetch_window(
    self, start: int, stop: int, columns: Sequence[int]
) -> list[ColumnChunk]:
    """Model rows `start:stop` of the given columns, in display order."""
    state = self._state
    if state.source is None:
        return []
    if state.row_order is None:
        return state.source.fetch(start, stop, columns)
    assert isinstance(state.source, PermutableSource)
    return state.source.fetch_rows(state.row_order[start:stop], columns)

prefetch

prefetch(first_row: int, last_row: int, columns: Sequence[int] | None = None)

Convert the blocks covering rows first_row..last_row (any thread).

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def prefetch(
    self, first_row: int, last_row: int, columns: Sequence[int] | None = None
):
    """Convert the blocks covering rows `first_row..last_row` (any thread)."""
    state = self._state
    if state.source is None or state.row_count == 0:
        return
    last_row = min(last_row, state.row_count - 1)
    cols = range(len(state.columns)) if columns is None else columns
    for block in range(
        first_row // self._chunk_rows, last_row // self._chunk_rows + 1
    ):
        for col in cols:
            self._cache.get_or_build(block, col, self._build_chunk)

sort

sort(ncol: int, order: SortOrder = ASCENDING)

Sort by column ncol (see sort_mode); unsupported sources are ignored.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def sort(self, ncol: int, order: constants.SortOrder = constants.ASCENDING):
    """Sort by column `ncol` (see `sort_mode`); unsupported sources are ignored."""
    source = self._state.source
    if not 0 <= ncol < self.columnCount():
        return
    descending = order == constants.DESCENDING
    if self._sort_mode == "permutation":
        if (permutation := self.compute_sort_permutation(ncol, order)) is not None:
            self.apply_sort_permutation(permutation)
        return
    if not isinstance(source, SortableSource):
        return
    new = source.sort(ncol, descending=descending)
    with self.change_layout():
        self.df = new

source_row

source_row(row: int) -> int

Row position in the frame of the model's row row (differs once sorted).

Source code in src/cutepandas/pandasmodels/dataframemodel.py
def source_row(self, row: int) -> int:
    """Row position in the frame of the model's row `row` (differs once sorted)."""
    order = self._state.row_order
    return row if order is None else int(order[row])

:fa-brands-github: Show source on GitHub