Skip to content

TopValuesModel

Base classes

Name Children Inherits
TranslatableHeaderMixin
cutepandas.utils.i18n
Item model mixin: horizontal header labels from `HEADER`, translated.
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

⋔ Inheritance diagram

graph TD
  1974361777["columnprofilewidget.TopValuesModel"]
  1559661765["i18n.TranslatableHeaderMixin"]
  3771062457["i18n.TranslateMixin"]
  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"]
  1559661765 --> 1974361777
  3771062457 --> 1559661765
  2751680898 --> 3771062457
  656210031 --> 1974361777
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  1115338939 --> 656210031
  930985301 --> 1115338939
  13951691 --> 930985301
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: TranslatableHeaderMixin, AbstractTableModel

Most frequent values of a column: value, count and share of all rows.

Source code in src/cutepandas/pandaswidgets/columnprofilewidget.py
class TopValuesModel(TranslatableHeaderMixin, core.AbstractTableModel):
    """Most frequent values of a column: value, count and share of all rows."""

    HEADER: ClassVar[list[str]] = [N_("Value"), N_("Count"), N_("Share")]

    def __init__(
        self,
        top: Sequence[tuple[Any, int]] = (),
        total: int = 0,
        *,
        translate: Translate | None = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._translate = translate
        self._top = list(top)
        self._total = total
        self._max = max((count for _, count in self._top), default=0)

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

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

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        value, count = self._top[index.row()]
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                return format_name(value)
            case constants.DISPLAY_ROLE, 1:
                return str(count)
            case constants.DISPLAY_ROLE, 2:
                return f"{count / self._total:.1%}" if self._total else ""
            case constants.ALIGNMENT_ROLE, 1 | 2:
                return constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
            case DataFrameModel.Roles.NormalizedRole, 2:
                return count / self._max if self._max else None
            case constants.USER_ROLE, _:
                return value
        return None

:fa-brands-github: Show source on GitHub