pandasmodels
Class info¶
Re-exported classes¶
| Class | Module | Description |
|---|---|---|
| CategoryModel | cutepandas.pandasmodels.categorymodel | One row per category of a column, with its code and number of rows, read-only. |
| CellChunk | cutepandas.pandasmodels.dataframemodel | One converted block of a column: everything data() returns, per row. |
| DataFrameHolder | cutepandas.pandasmodels.dataframeholder | Holds the current frame for several models / widgets (GUI thread only). |
| DataFrameModel | cutepandas.pandasmodels.dataframemodel | Virtual table model for pandas, pyarrow and polars frames. |
| HorizontalHeaderModel | cutepandas.pandasmodels.dataframemodel | Column labels: one row per pandas column level; one row for other backends. |
| PandasCategoryListModel | cutepandas.pandasmodels.pandascategorylistmodel | CategoryModel for a pandas category column that also edits it. |
| PandasColumnListModel | cutepandas.pandasmodels.pandascolumnlistmodel | SchemaModel for pandas frames that also edits them. |
| PandasEvalFilterProxyModel | cutepandas.pandasmodels.pandasindexfilterproxymodel | Filters a DataFrame by a boolean DataFrame.eval expression, like 'a > 10'. |
| PandasIndexListModel | cutepandas.pandasmodels.pandascolumnlistmodel | Index levels of a pandas frame: name, index type, monotonic (editable). |
| PandasMultiStringColumnFilterProxyModel | cutepandas.pandasmodels.pandasindexfilterproxymodel | Shows rows where every given column starts with its given prefix. |
| PandasStringColumnFilterProxyModel | cutepandas.pandasmodels.pandasindexfilterproxymodel | Filters a DataFrame by df.iloc[:, column].str. |
| SchemaModel | cutepandas.pandasmodels.schemamodel | One row per column of a frame (any backend), read-only. |
| SortPermutation | cutepandas.pandasmodels.dataframemodel | A display-only sort order, see DataFrameModel.compute_sort_permutation. |
| VerticalHeaderModel | cutepandas.pandasmodels.dataframemodel | Row labels: one column per pandas index level; row numbers for other backends. |
🛈 DocStrings¶
Pandas models.
CutePandas contains a range of tools to work with pandas data structures.
Models¶
DataFrameModel is a virtual table model for pandas, pyarrow and polars frames. It
reads through a FrameSource (see cutepandas.frames) and converts only the visible
window: rows are fetched in blocks per column, formatted once by per-dtype formatters
and kept in a bounded, thread-safe LRU cache. pandas MultiIndex columns are shown by
joining the levels with a separator (a Qt Property).
VerticalHeaderModel / HorizontalHeaderModel show one cell per index level; the
DataFrameViewer widget combines them with the data table (synced on scrolling /
resizing).
SchemaModel lists the columns of any frame (dtype, icon, null count, size),
CategoryModel the categories of a categorical / enum column. Their pandas
subclasses PandasColumnListModel and PandasCategoryListModel also edit the frame;
PandasIndexListModel shows (and edits) the index levels.
Models can share one frame through a DataFrameHolder: a change made through one model (rename, dtype change, sorting, ...) shows up in all others.
Proxies¶
Since working with pandas often means working with tables containing several hundred thousands of rows, the default QSortFilterProxyModel does not work that well.
CutePandas includes several proxy models which try to improve this.
PandasStringColumnFilterProxyModel can be used to filter a column based on a search string. Instead of looping over the cells, a filter index is built using NumPy operations. This makes filtering super fast, even with several thousands of rows. A quick benchmark showed an almost 100x (!!) performance increase compared to QSortFilterProxyModel.
PandasEvalFilterProxyModel works in a similar way, but filtering is done by a
DataFrame.eval expression (example: 'a > 10' shows all rows where the value of
column "a" is greater than 10).
PandasMultiStringColumnFilterProxyModel shows the rows where several columns each start with a given prefix.
The proxies compute their masks with pandas: they need a pandas frame.
CategoryModel
¶
Bases: TranslatableHeaderMixin, DataFrameHolderMixin, ModelMixin, AbstractTableModel
One row per category of a column, with its code and number of rows, read-only.
Works for pandas category and dictionary-encoded pd.ArrowDtype columns, polars
Enum (fixed categories) and Categorical (the values present), and pyarrow
dictionary columns. Other columns show no rows. Categories and counts are computed
once per frame (one value_counts scan).
Source code in src/cutepandas/pandasmodels/categorymodel.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
__init__
¶
Show the categories of column col (a label).
translate maps the English labels (header, tooltip) to the shown text.
Source code in src/cutepandas/pandasmodels/categorymodel.py
CellChunk
dataclass
¶
One converted block of a column: everything data() returns, per row.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
scaled
class-attribute
instance-attribute
¶
True once normalized / colors were computed (both stay None if the
column is not numeric / boolean).
DataFrameHolder
¶
Bases: Object
Holds the current frame for several models / widgets (GUI thread only).
Frame operations like rename or astype return a new frame instead of changing
the old one. Models that share a holder always see the current frame: replacing it
emits about_to_change and changed, and every model except the one that made the
change (the origin) resets itself.
Accepts a pandas DataFrame / Series (a Series is stored as a one-column frame), a
pyarrow Table, a polars DataFrame or any FrameSource. df is the native frame,
source the backend-agnostic adapter the table models read through.
row_order is an optional display order (row positions, see set_row_order):
a sort that only changes what the views show, without copying the frame.
Replacing the frame clears it.
Source code in src/cutepandas/pandasmodels/dataframeholder.py
df
property
writable
¶
The native frame (pandas DataFrame, pyarrow Table, polars DataFrame).
row_order
property
¶
Display order of the rows (row positions), None for the frame's order.
set_df
¶
Replace the frame. origin is passed on with the signals.
set_row_order
¶
Show the rows in another order without touching the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order
|
Any
|
One row position per row (a permutation, e.g. from
|
required |
origin
|
Any
|
Passed on with the signals. |
None
|
Source code in src/cutepandas/pandasmodels/dataframeholder.py
DataFrameModel
¶
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'srow_order; the frame is not copied or changed, and every visible block is read through the order (PermutableSource.fetch_rows). Rowiof the model is rowsource_row(i)of the frame. Replacing the frame orclear_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 | |
row_order
property
¶
Display order of the rows (row positions), None if not permuted.
Roles
¶
Bases: IntEnum
Role names.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
ColumnNameRole
class-attribute
instance-attribute
¶
Column label.
DTypeRole
class-attribute
instance-attribute
¶
Backend dtype object of the column.
IsNullRole
class-attribute
instance-attribute
¶
True if the cell is missing.
KindRole
class-attribute
instance-attribute
¶
Kind of the column.
NormalizedRole
class-attribute
instance-attribute
¶
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
|
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'
|
args
|
Passed on to the Qt base class. |
()
|
|
kwargs
|
Passed on to the Qt base class. |
{}
|
Source code in src/cutepandas/pandasmodels/dataframemodel.py
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
can_sort
¶
True if the source supports sorting in the current sort_mode.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
clear_sort
¶
Back to the frame's own row order (drops a display-only sort).
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
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
prefetch
¶
Convert the blocks covering rows first_row..last_row (any thread).
Source code in src/cutepandas/pandasmodels/dataframemodel.py
sort
¶
Sort by column ncol (see sort_mode); unsupported sources are ignored.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
source_row
¶
Row position in the frame of the model's row row (differs once sorted).
HorizontalHeaderModel
¶
Bases: DataFrameHolderMixin, AbstractTableModel
Column labels: one row per pandas column level; one row for other backends.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
PandasCategoryListModel
¶
Bases: CategoryModel
CategoryModel for a pandas category column that also edits it.
Rename, reorder (drag and drop, sorting), add categories, toggle ordered,
remove unused ones. USER_ROLE returns the rows of a category.
Source code in src/cutepandas/pandasmodels/pandascategorylistmodel.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
PandasColumnListModel
¶
Bases: SchemaModel
SchemaModel for pandas frames that also edits them.
Rename (column 0), change the dtype (column 1, see DTypeDelegate), remove,
reorder by drag and drop or by sorting, append columns and move index levels into
the columns (drop from PandasIndexListModel).
Source code in src/cutepandas/pandasmodels/pandascolumnlistmodel.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
add_feature
¶
Append a Series as a new column (named after the Series).
Source code in src/cutepandas/pandasmodels/pandascolumnlistmodel.py
PandasEvalFilterProxyModel
¶
Bases: BasePandasIndexFilterProxyModel
Filters a DataFrame by a boolean DataFrame.eval expression, like 'a > 10'.
Invalid expressions, or ones that don't evaluate to a boolean Series, show no rows.
Source code in src/cutepandas/pandasmodels/pandasindexfilterproxymodel.py
PandasIndexListModel
¶
Bases: TranslatableHeaderMixin, DataFrameHolderMixin, ModelMixin, AbstractTableModel
Index levels of a pandas frame: name, index type, monotonic (editable).
Labels go through translate (see cutepandas.utils.i18n).
Source code in src/cutepandas/pandasmodels/pandascolumnlistmodel.py
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 | |
removeRows
¶
Remove index levels (the rows of this model) from the DataFrame.
Source code in src/cutepandas/pandasmodels/pandascolumnlistmodel.py
to_datetime
¶
Convert the index (or one level of a MultiIndex) to datetimes.
Source code in src/cutepandas/pandasmodels/pandascolumnlistmodel.py
PandasMultiStringColumnFilterProxyModel
¶
Bases: BasePandasIndexFilterProxyModel
Shows rows where every given column starts with its given prefix.
Filters are a mapping of column label to prefix; empty prefixes are ignored.
Source code in src/cutepandas/pandasmodels/pandasindexfilterproxymodel.py
PandasStringColumnFilterProxyModel
¶
Bases: BasePandasIndexFilterProxyModel
Filters a DataFrame by df.iloc[:, column].str.
Source code in src/cutepandas/pandasmodels/pandasindexfilterproxymodel.py
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 | |
SchemaModel
¶
Bases: TranslatableHeaderMixin, DataFrameHolderMixin, ModelMixin, AbstractTableModel
One row per column of a frame (any backend), read-only.
Null counts and sizes are computed on first display of a row and cached until the
frame changes (a null count scans the column; polars and pyarrow keep it in
metadata). Sort it with a SortFilterProxyModel on SORT_ROLE.
Labels (HEADER, tooltips) go through translate (see cutepandas.utils.i18n).
Source code in src/cutepandas/pandasmodels/schemamodel.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
Roles
¶
Bases: IntEnum
Role names.
Source code in src/cutepandas/pandasmodels/schemamodel.py
SortPermutation
dataclass
¶
A display-only sort order, see DataFrameModel.compute_sort_permutation.
Source code in src/cutepandas/pandasmodels/dataframemodel.py
VerticalHeaderModel
¶
Bases: DataFrameHolderMixin, AbstractTableModel
Row labels: one column per pandas index level; row numbers for other backends.