Skip to content

Split

Base classes

Name Children Inherits
_ExpandOp
datacook.engine.ops.strings
Text -> several new columns: `df.join(df["a"].str.<method>(...).add_prefix("a_"))`.

⋔ Inheritance diagram

graph TD
  930121898["strings.Split"]
  4238617847["strings._ExpandOp"]
  1539482213["base.Op"]
  2751680898["builtins.object"]
  4238617847 --> 930121898
  1539482213 --> 4238617847
  2751680898 --> 1539482213

🛈 DocStrings

Bases: _ExpandOp

Source code in src/datacook/engine/ops/strings.py
class Split(_ExpandOp):
    id = "str_split"
    title = _("Split text into columns")
    description = _("Split text at a separator into new columns.")
    icon = "mdi.call-split"
    method = "split"

    class Params(base.Params):
        column: str = Field(title=N_("Column"), json_schema_extra={"widget": "column"})
        pat: str | None = Field(None, title=N_("Separator"), description=N_("Empty: whitespace."))
        n: int = Field(-1, title=N_("Maximum splits"), ge=-1, description=N_("-1: all"))
        regex: bool = Field(False, title=N_("Regular expression"))
        prefix: str | None = Field(
            None,
            title=N_("Prefix of the new columns"),
            min_length=1,
            description=N_("Default: the column name and '_'."),
        )

    @classmethod
    def call(cls, params: Params) -> tuple[list[Any], dict[str, Any]]:
        args = [] if params.pat is None else [params.pat]
        kwargs: dict[str, Any] = {}
        if params.n != -1:
            kwargs["n"] = params.n
        if params.regex:
            kwargs["regex"] = True
        kwargs["expand"] = True
        return args, kwargs

    @classmethod
    def params_from(
        cls, column: str, args: list[Any], kwargs: dict[str, Any], prefix: str
    ) -> Params:
        if kwargs.pop("expand", None) is not True or len(args) > 1:
            msg = "split: expected expand=True"
            raise ValueError(msg)
        return cls.Params(
            column=column,
            pat=args[0] if args else None,
            prefix=_prefix_param(prefix, column),
            **kwargs,
        )

:fa-brands-github: Show source on GitHub