Skip to content

stats

Class info

Classes

Name Children Inherits
Bin
datacook.engine.ops.stats
`pd.cut`: equal-width bins (a number) or bins between edges (a list).
    Correlation
    datacook.engine.ops.stats
      CorrelationWith
      datacook.engine.ops.stats
      `df.corrwith(other)`: columns matched by name, rows by position (no index).
        Covariance
        datacook.engine.ops.stats
          Describe
          datacook.engine.ops.stats
            Mode
            datacook.engine.ops.stats
              Rank
              datacook.engine.ops.stats
                SubsetCallOp
                datacook.engine.ops.stats
                A frame method on all columns or on the `columns` subset, with a new table as result.
                ValueCounts
                datacook.engine.ops.stats
                  Variance
                  datacook.engine.ops.stats

                    🛈 DocStrings

                    Statistics ops: describe, variances, correlations, modes, value counts, ranks, bins.

                    Correlations also with another table; bins via pd.cut.

                    Summary results are new tables like every other step: row labels become a column (statistic / column), e.g. df.corr().reset_index(names="column"). Rank and binning are column ops (ops/series.py): results go to <column>_rank / <column>_bin by default.

                    Bin

                    Bases: SeriesOp

                    pd.cut: equal-width bins (a number) or bins between edges (a list).

                    Source code in src/datacook/engine/ops/stats.py
                    class Bin(series.SeriesOp):
                        """`pd.cut`: equal-width bins (a number) or bins between edges (a list)."""
                    
                        id = "cut"
                        title = _("Bin values")
                        description = _(
                            "Put numbers into intervals: a number of equal-width bins, or bins between given"
                            " edges (histogram classes)."
                        )
                        category = categories.STATS
                        icon = "mdi.chart-histogram"
                        function = "pd.cut"
                        positional = ("bins",)
                    
                        class Params(series.ColumnParams):
                            suffix: str = Field(
                                "_bin",
                                title=N_("Suffix for new columns"),
                                description=N_("Empty: replace the columns. Otherwise the results are new columns."),
                            )
                            bins: int | list[float] = Field(
                                title=N_("Bins"),
                                description=N_("A number of equal-width bins, or the bin edges (ascending)."),
                            )
                            right: bool = Field(True, title=N_("Bins include their right edge"))
                            include_lowest: bool = Field(False, title=N_("First bin includes its left edge"))
                            labels: list[str] | None = Field(
                                None, title=N_("Labels"), description=N_("One per bin; empty: the intervals.")
                            )
                            precision: int = Field(3, title=N_("Precision of the interval labels"), ge=0)
                    
                            @model_validator(mode="after")
                            def _check_bins(self) -> Self:
                                if isinstance(self.bins, int):
                                    if self.bins < 1:
                                        raise ValueError("at least one bin")
                                    n = self.bins
                                else:
                                    if (
                                        len(self.bins) < 2  # noqa: PLR2004
                                        or any(a >= b for a, b in zip(self.bins, self.bins[1:]))
                                    ):
                                        raise ValueError("bin edges: at least two, ascending")
                                    n = len(self.bins) - 1
                                if self.labels is not None and len(self.labels) != n:
                                    raise ValueError(f"{n} labels needed")
                                return self
                    

                    CorrelationWith

                    Bases: CallOp

                    df.corrwith(other): columns matched by name, rows by position (no index).

                    Pearson only: pandas computes Spearman / Kendall here with scipy (no dependency).

                    Source code in src/datacook/engine/ops/stats.py
                    class CorrelationWith(base.CallOp):
                        """`df.corrwith(other)`: columns matched by name, rows by position (no index).
                    
                        Pearson only: pandas computes Spearman / Kendall here with scipy (no dependency).
                        """
                    
                        id = "corrwith"
                        title = _("Correlation with another table")
                        description = _(
                            "Pearson correlation of each column with the column of the same name in a second"
                            " table (rows matched by position)."
                        )
                        category = categories.STATS
                        icon = "mdi.chart-scatter-plot"
                        n_inputs = 2
                        method = "corrwith"
                        always = ("numeric_only",)
                        then = '.rename_axis("column").reset_index(name="correlation")'
                    
                        class Params(base.Params):
                            drop: bool = Field(False, title=N_("Drop columns missing in one table"))
                            numeric_only: bool = _numeric_only()
                    
                        @classmethod
                        def finish(cls, params: Any, result: Any) -> Any:
                            return result.rename_axis("column").reset_index(name="correlation")
                    

                    SubsetCallOp

                    Bases: CallOp

                    A frame method on all columns or on the columns subset, with a new table as result.

                    out = df[["x", "y"]].corr(method="spearman").reset_index(names="column") (then is the tail, finish() its execution). columns is never passed to the method.

                    Source code in src/datacook/engine/ops/stats.py
                    class SubsetCallOp(base.CallOp):
                        """A frame method on all columns or on the `columns` subset, with a new table as result.
                    
                        `out = df[["x", "y"]].corr(method="spearman").reset_index(names="column")` (`then` is
                        the tail, `finish()` its execution). `columns` is never passed to the method.
                        """
                    
                        category = categories.STATS
                        icon = "mdi.chart-box-outline"
                    
                        @classmethod
                        def call_args(cls, params: base.Params) -> tuple[list[Any], dict[str, Any]]:
                            args, kwargs = base.call_arguments(params, cls.positional, cls.always, ("columns",))
                            return args, {**cls.fixed, **kwargs}
                    
                        @classmethod
                        def apply(cls, params: Any, *inputs: Frame) -> Frame:
                            frame = inputs[0]
                            if params.columns is not None:
                                missing = [c for c in params.columns if c not in frame.columns]
                                if missing:
                                    raise base.OpError(f"unknown columns: {missing}")
                                frame = frame[params.columns]
                            args, kwargs = cls.call_args(params)
                            assert cls.method is not None
                            result = cls.finish(params, getattr(frame, cls.method)(*args, **kwargs))
                            if not frames.is_frame(result):
                                msg = f"{cls.id} returned {type(result).__name__}, not a frame"
                                raise base.OpError(msg)
                            return result
                    
                        @classmethod
                        def to_code(cls, params: Any, inputs: Sequence[str], output: str) -> str:
                            source = inputs[0]
                            if params.columns is not None:
                                source = f"{source}[{base.literal(params.columns)}]"
                            args, kwargs = cls.call_args(params)
                            func = f"{source}.{cls.method}"
                            width = len(output) + 3 + len(cls.then)  # the tail goes on the last line
                            call = base.format_call(func, list(map(base.literal, args)), kwargs, width)
                            return f"{output} = {call}{cls.then}"
                    
                        @classmethod
                        def from_code(cls, stmt: ast.stmt, bound: Mapping[str, Any]) -> base.Match | None:  # noqa: PLR0911
                            assign = base.assignment(stmt)
                            if assign is None:
                                return None
                            output, value = assign
                            if cls.then:
                                value = base.strip_tail(value, cls.then)
                            chain = series.unchain(value) if value is not None else None
                            if chain is None:
                                return None
                            root, links = chain
                            if not (isinstance(root, ast.Name) and root.id in bound and 1 <= len(links) <= 2):  # noqa: PLR2004
                                return None
                            call = links[-1]
                            if call.kind != "call" or call.name != cls.method or "columns" in call.kwargs:
                                return None
                            try:
                                columns = series.item_key(links[0]) if len(links) == 2 else None  # noqa: PLR2004
                                args, kwargs = call.literal_args()
                            except ValueError:  # not literal: user code
                                return None
                            if columns is not None and not isinstance(columns, list):
                                return None
                            params = cls.params_from_call(args, {**kwargs, "columns": columns})
                            return None if params is None else base.Match(params, (root.id,), output)