|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from janitor.utils import import_message |
| 4 | + |
| 5 | +from .clean_names import _clean_expr_names |
| 6 | + |
| 7 | +try: |
| 8 | + import polars as pl |
| 9 | +except ImportError: |
| 10 | + import_message( |
| 11 | + submodule="polars", |
| 12 | + package="polars", |
| 13 | + conda_channel="conda-forge", |
| 14 | + pip_install=True, |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +@pl.api.register_expr_namespace("janitor") |
| 19 | +class PolarsExpr: |
| 20 | + def __init__(self, expr: pl.Expr) -> pl.Expr: |
| 21 | + self._expr = expr |
| 22 | + |
| 23 | + def clean_names( |
| 24 | + self, |
| 25 | + strip_underscores: str | bool = None, |
| 26 | + case_type: str = "lower", |
| 27 | + remove_special: bool = False, |
| 28 | + strip_accents: bool = False, |
| 29 | + enforce_string: bool = False, |
| 30 | + truncate_limit: int = None, |
| 31 | + ) -> pl.Expr: |
| 32 | + """ |
| 33 | + Clean the labels in a polars Expression. |
| 34 | +
|
| 35 | + Examples: |
| 36 | + >>> import polars as pl |
| 37 | + >>> import janitor.polars |
| 38 | + >>> df = pl.DataFrame({"raw": ["Abçdê fgí j"]}) |
| 39 | + >>> df |
| 40 | + shape: (1, 1) |
| 41 | + ┌─────────────┐ |
| 42 | + │ raw │ |
| 43 | + │ --- │ |
| 44 | + │ str │ |
| 45 | + ╞═════════════╡ |
| 46 | + │ Abçdê fgí j │ |
| 47 | + └─────────────┘ |
| 48 | +
|
| 49 | + Clean the column values: |
| 50 | + >>> df.with_columns(pl.col("raw").janitor.clean_names(strip_accents=True)) |
| 51 | + shape: (1, 1) |
| 52 | + ┌─────────────┐ |
| 53 | + │ raw │ |
| 54 | + │ --- │ |
| 55 | + │ str │ |
| 56 | + ╞═════════════╡ |
| 57 | + │ abcde_fgi_j │ |
| 58 | + └─────────────┘ |
| 59 | +
|
| 60 | + !!! info "New in version 0.28.0" |
| 61 | +
|
| 62 | + Args: |
| 63 | + strip_underscores: Removes the outer underscores |
| 64 | + from all labels in the expression. |
| 65 | + Default None keeps outer underscores. |
| 66 | + Values can be either 'left', 'right' |
| 67 | + or 'both' or the respective shorthand 'l', |
| 68 | + 'r' and True. |
| 69 | + case_type: Whether to make the labels in the expression lower or uppercase. |
| 70 | + Current case may be preserved with 'preserve', |
| 71 | + while snake case conversion (from CamelCase or camelCase only) |
| 72 | + can be turned on using "snake". |
| 73 | + Default 'lower' makes all characters lowercase. |
| 74 | + remove_special: Remove special characters from the values in the expression. |
| 75 | + Only letters, numbers and underscores are preserved. |
| 76 | + strip_accents: Whether or not to remove accents from |
| 77 | + the expression. |
| 78 | + enforce_string: Whether or not to cast the expression to a string type. |
| 79 | + truncate_limit: Truncates formatted labels in the expression to |
| 80 | + the specified length. Default None does not truncate. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + A polars Expression. |
| 84 | + """ |
| 85 | + return _clean_expr_names( |
| 86 | + obj=self._expr, |
| 87 | + strip_accents=strip_accents, |
| 88 | + strip_underscores=strip_underscores, |
| 89 | + case_type=case_type, |
| 90 | + remove_special=remove_special, |
| 91 | + enforce_string=enforce_string, |
| 92 | + truncate_limit=truncate_limit, |
| 93 | + ) |
0 commit comments