Skip to content

VerticalHeaderModel

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
  2308448081["dataframemodel.VerticalHeaderModel"]
  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 --> 2308448081
  2751680898 --> 103874424
  656210031 --> 2308448081
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  1115338939 --> 656210031
  930985301 --> 1115338939
  13951691 --> 930985301
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: DataFrameHolderMixin, AbstractTableModel

Row labels: one column per pandas index level; row numbers for other backends.

Source code in src/cutepandas/pandasmodels/dataframemodel.py
class VerticalHeaderModel(DataFrameHolderMixin, core.AbstractTableModel):
    """Row labels: one column per pandas index level; row numbers for other backends."""

    def __init__(self, df: Any = None, **kwargs):
        super().__init__(**kwargs)
        self._init_holder(df)
        # A display-only sort (holder.row_order) moves the row labels too.
        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)

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

    def _on_row_order_changed(self, origin: Any):
        self.layoutChanged.emit()

    def _row(self, row: int) -> int:
        order = self.holder.row_order
        return row if order is None else int(order[row])

    def _index(self) -> pd.Index | None:
        df = self.df
        return df.index if isinstance(df, pd.DataFrame) else None

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        if self.df is None or (parent is not None and parent.isValid()):
            return 0
        index = self._index()
        return 1 if index is None else index.nlevels

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

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if self.df is None or not index.isValid():
            return None
        match role:
            case constants.DISPLAY_ROLE | constants.TOOLTIP_ROLE:
                pd_index = self._index()
                row = self._row(index.row())
                if pd_index is None:
                    return str(row)
                value = pd_index[row]
                if isinstance(pd_index, pd.MultiIndex):
                    value = value[index.column()]
                return str(value)
        return None

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match role, orientation:
            case constants.DISPLAY_ROLE | constants.TOOLTIP_ROLE, constants.HORIZONTAL:
                pd_index = self._index()
                if pd_index is None:
                    return "" if section == 0 and self.df is not None else None
                if not 0 <= section < pd_index.nlevels:
                    return None
                name = pd_index.names[section]
                return "" if name is None else str(name)
        return None

:fa-brands-github: Show source on GitHub