Skip to content

PandasMultiStringColumnFilterProxyModel

Base classes

Name Children Inherits
BasePandasIndexFilterProxyModel
cutepandas.pandasmodels.pandasindexfilterproxymodel
Base proxy to filter a pandas DataFrame based on a calculated boolean index.

⋔ Inheritance diagram

graph TD
  2383897471["pandasindexfilterproxymodel.PandasMultiStringColumnFilterProxyModel"]
  1453093054["pandasindexfilterproxymodel.BasePandasIndexFilterProxyModel"]
  1274898529["core.AbstractProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  2723940761["QtCore.QAbstractProxyModel"]
  930985301["QtCore.QAbstractItemModel"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  1453093054 --> 2383897471
  1274898529 --> 1453093054
  2431222121 --> 1274898529
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2723940761 --> 1274898529
  930985301 --> 2723940761
  13951691 --> 930985301
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

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
class PandasMultiStringColumnFilterProxyModel(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.
    """

    def __init__(self, **kwargs):
        self._filters: dict[str, str] = {}
        super().__init__(**kwargs)

    def compute_filter(self, df: pd.DataFrame) -> np.ndarray | None:
        active = {k: v for k, v in self._filters.items() if v}
        if not active:
            return None
        mask = np.ones(len(df), dtype=bool)
        for column, prefix in active.items():
            if column not in df.columns:
                return np.zeros(len(df), dtype=bool)
            series = _as_string_series(df[column])
            mask &= _to_mask(series.str.startswith(prefix, na=False), len(df))
        return mask

    def set_filters(self, filters: dict[str, str]):
        self._filters = dict(filters)
        if self.sourceModel() is not None:
            self.invalidate_filter()

    def get_filters(self) -> dict[str, str]:
        return self._filters

    filters = core.Property(
        dict,
        get_filters,
        set_filters,
        doc="Dictionary containing a Column->Prefix mapping",
    )

:fa-brands-github: Show source on GitHub