o
    Ή­§iF:  γ                   @  sR   d dl mZ d dlmZ d dlmZ er d dlmZ d dlm	Z	 G dd dZ
dS )	ι    )Ϊannotations)ΪTYPE_CHECKING)Ϊ	wrap_expr)ΪCallable)ΪExprc                   @  s   e Zd ZdZdZd)ddZd*d	d
Zd+ddZd,ddZd-ddZ	d*ddZ
d*ddZd+ddZd,ddZddd.d$d%Zd-d&d'Zd(S )/ΪExprNameNameSpacez;Namespace for expressions that operate on expression names.ΪnameΪexprr   ΪreturnΪNonec                 C  s   |j | _ d S )N)Ϊ_pyexpr)Ϊselfr	   © r   ϊG/home/app/Keep/.python/lib/python3.10/site-packages/polars/expr/name.pyΪ__init__   s   zExprNameNameSpace.__init__c                 C  σ   t | j ‘ S )uΥ  
        Keep the original root name of the expression.

        See Also
        --------
        Expr.alias
        map

        Examples
        --------
        Prevent errors due to potential duplicate column names.

        >>> df = pl.DataFrame(
        ...     {
        ...         "a": [1, 2],
        ...         "b": [3, 4],
        ...     }
        ... )
        >>> df.select((pl.lit(10) / pl.all()).name.keep())
        shape: (2, 2)
        ββββββββ¬βββββββββββ
        β a    β b        β
        β ---  β ---      β
        β f64  β f64      β
        ββββββββͺβββββββββββ‘
        β 10.0 β 3.333333 β
        β 5.0  β 2.5      β
        ββββββββ΄βββββββββββ

        Undo an alias operation.

        >>> df.with_columns((pl.col("a") * 9).alias("c").name.keep())
        shape: (2, 2)
        βββββββ¬ββββββ
        β a   β b   β
        β --- β --- β
        β i64 β i64 β
        βββββββͺββββββ‘
        β 9   β 3   β
        β 18  β 4   β
        βββββββ΄ββββββ
        )r   r   Z	name_keep©r   r   r   r   Ϊkeep   s   +zExprNameNameSpace.keepΪfunctionϊCallable[[str], str]c                 C  σ   t | j |‘S )u°  
        Rename the output of an expression by mapping a function over the root name.

        Parameters
        ----------
        function
            Function that maps a root name to a new name.

        See Also
        --------
        keep
        prefix
        suffix
        replace

        Examples
        --------
        Remove a common suffix and convert to lower case.

        >>> df = pl.DataFrame(
        ...     {
        ...         "A_reverse": [3, 2, 1],
        ...         "B_reverse": ["z", "y", "x"],
        ...     }
        ... )
        >>> df.with_columns(
        ...     pl.all()
        ...     .reverse()
        ...     .name.map(lambda c: c.removesuffix("_reverse").lower())
        ... )
        shape: (3, 4)
        βββββββββββββ¬ββββββββββββ¬ββββββ¬ββββββ
        β A_reverse β B_reverse β a   β b   β
        β ---       β ---       β --- β --- β
        β i64       β str       β i64 β str β
        βββββββββββββͺββββββββββββͺββββββͺββββββ‘
        β 3         β z         β 1   β x   β
        β 2         β y         β 2   β y   β
        β 1         β x         β 3   β z   β
        βββββββββββββ΄ββββββββββββ΄ββββββ΄ββββββ
        )r   r   Zname_map©r   r   r   r   r   ΪmapB   s   *zExprNameNameSpace.mapΪprefixΪstrc                 C  r   )uΘ  
        Add a prefix to the root column name of the expression.

        Parameters
        ----------
        prefix
            Prefix to add to the root column name.

        See Also
        --------
        suffix
        map

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "a": [1, 2, 3],
        ...         "b": ["x", "y", "z"],
        ...     }
        ... )
        >>> df.with_columns(pl.all().reverse().name.prefix("reverse_"))
        shape: (3, 4)
        βββββββ¬ββββββ¬ββββββββββββ¬ββββββββββββ
        β a   β b   β reverse_a β reverse_b β
        β --- β --- β ---       β ---       β
        β i64 β str β i64       β str       β
        βββββββͺββββββͺββββββββββββͺββββββββββββ‘
        β 1   β x   β 3         β z         β
        β 2   β y   β 2         β y         β
        β 3   β z   β 1         β x         β
        βββββββ΄ββββββ΄ββββββββββββ΄ββββββββββββ
        )r   r   Zname_prefix©r   r   r   r   r   r   n   σ   "zExprNameNameSpace.prefixΪsuffixc                 C  r   )uΘ  
        Add a suffix to the root column name of the expression.

        Parameters
        ----------
        suffix
            Suffix to add to the root column name.

        See Also
        --------
        prefix
        map

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "a": [1, 2, 3],
        ...         "b": ["x", "y", "z"],
        ...     }
        ... )
        >>> df.with_columns(pl.all().reverse().name.suffix("_reverse"))
        shape: (3, 4)
        βββββββ¬ββββββ¬ββββββββββββ¬ββββββββββββ
        β a   β b   β a_reverse β b_reverse β
        β --- β --- β ---       β ---       β
        β i64 β str β i64       β str       β
        βββββββͺββββββͺββββββββββββͺββββββββββββ‘
        β 1   β x   β 3         β z         β
        β 2   β y   β 2         β y         β
        β 3   β z   β 1         β x         β
        βββββββ΄ββββββ΄ββββββββββββ΄ββββββββββββ
        )r   r   Zname_suffix©r   r   r   r   r   r      r   zExprNameNameSpace.suffixc                 C  r   )uπ  
        Make the root column name lowercase.

        See Also
        --------
        prefix
        suffix
        to_uppercase
        map

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "ColX": [1, 2, 3],
        ...         "ColY": ["x", "y", "z"],
        ...     }
        ... )
        >>> df.with_columns(pl.all().name.to_lowercase())
        shape: (3, 4)
        ββββββββ¬βββββββ¬βββββββ¬βββββββ
        β ColX β ColY β colx β coly β
        β ---  β ---  β ---  β ---  β
        β i64  β str  β i64  β str  β
        ββββββββͺβββββββͺβββββββͺβββββββ‘
        β 1    β x    β 1    β x    β
        β 2    β y    β 2    β y    β
        β 3    β z    β 3    β z    β
        ββββββββ΄βββββββ΄βββββββ΄βββββββ
        )r   r   Zname_to_lowercaser   r   r   r   Ϊto_lowercaseΆ   σ   zExprNameNameSpace.to_lowercasec                 C  r   )uπ  
        Make the root column name uppercase.

        See Also
        --------
        prefix
        suffix
        to_lowercase
        map

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "ColX": [1, 2, 3],
        ...         "ColY": ["x", "y", "z"],
        ...     }
        ... )
        >>> df.with_columns(pl.all().name.to_uppercase())
        shape: (3, 4)
        ββββββββ¬βββββββ¬βββββββ¬βββββββ
        β ColX β ColY β COLX β COLY β
        β ---  β ---  β ---  β ---  β
        β i64  β str  β i64  β str  β
        ββββββββͺβββββββͺβββββββͺβββββββ‘
        β 1    β x    β 1    β x    β
        β 2    β y    β 2    β y    β
        β 3    β z    β 3    β z    β
        ββββββββ΄βββββββ΄βββββββ΄βββββββ
        )r   r   Zname_to_uppercaser   r   r   r   Ϊto_uppercaseΧ   r    zExprNameNameSpace.to_uppercasec                 C  r   )aL  
        Rename fields of a struct by mapping a function over the field name(s).

        Notes
        -----
        This only takes effect for struct columns.

        Parameters
        ----------
        function
            Function that maps a field name to a new name.

        See Also
        --------
        prefix_fields
        suffix_fields

        Examples
        --------
        >>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
        >>> df.select(pl.col("x").name.map_fields(lambda x: x.upper())).schema
        Schema({'x': Struct({'A': Int64, 'B': Int64})})
        )r   r   Zname_map_fieldsr   r   r   r   Ϊ
map_fieldsψ   σ   zExprNameNameSpace.map_fieldsc                 C  r   )a%  
        Add a prefix to all field names of a struct.

        Notes
        -----
        This only takes effect for struct columns.

        Parameters
        ----------
        prefix
            Prefix to add to the field name.

        See Also
        --------
        map_fields
        suffix_fields

        Examples
        --------
        >>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
        >>> df.select(pl.col("x").name.prefix_fields("prefix_")).schema
        Schema({'x': Struct({'prefix_a': Int64, 'prefix_b': Int64})})
        )r   r   Zname_prefix_fieldsr   r   r   r   Ϊprefix_fields  r#   zExprNameNameSpace.prefix_fieldsF)ΪliteralΪpatternΪvaluer%   Ϊboolc                C  s   t | j |||‘S )u/  
        Replace matching regex/literal substring in the name with a new value.

        Parameters
        ----------
        pattern
            A valid regular expression pattern, compatible with the `regex crate
            <https://docs.rs/regex/latest/regex/>`_.
        value
            String that will replace the matched substring.
        literal
            Treat `pattern` as a literal string, not a regex.

        Notes
        -----
        * To modify regular expression behaviour (such as case-sensitivity) with flags,
          use the inline `(?iLmsuxU)` syntax. See the regex crate's section on
          `grouping and flags <https://docs.rs/regex/latest/regex/#grouping-and-flags>`_
          for additional information about the use of inline expression modifiers.

        * The dollar sign (`$`) is a special character related to capture groups; if you
          want to replace some target pattern with characters that include a literal `$`
          you should escape it by doubling it up as `$$`, or set `literal=True` if you
          do not need a full regular expression pattern match. Otherwise, you will be
          referencing a (potentially non-existent) capture group.

        See Also
        --------
        Expr.str.replace

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "n_foo": [1, 2, 3],
        ...         "n_bar": ["x", "y", "z"],
        ...     }
        ... )
        >>> df.select(pl.all().name.replace(r"^n_", "col_"))
        shape: (3, 2)
        βββββββββββ¬ββββββββββ
        β col_foo β col_bar β
        β ---     β ---     β
        β i64     β str     β
        βββββββββββͺββββββββββ‘
        β 1       β x       β
        β 2       β y       β
        β 3       β z       β
        βββββββββββ΄ββββββββββ
        >>> df.select(pl.all().name.replace(r"(a|e|i|o|u)", "@")).schema
        Schema({'n_f@@': Int64, 'n_b@r': String})

        Apply case-insensitive string replacement using the `(?i)` flag.

        >>> pl.DataFrame({"Foo": [1], "faz": [2]}).select(
        ...     pl.all().name.replace(r"(?i)^f", "b")
        ... )
        shape: (1, 2)
        βββββββ¬ββββββ
        β boo β baz β
        β --- β --- β
        β i64 β i64 β
        βββββββͺββββββ‘
        β 1   β 2   β
        βββββββ΄ββββββ

        Capture groups are supported. Use `$1` or `${1}` in the `value` string to refer
        to the first capture group in the pattern, `$2` or `${2}` to refer to the
        second capture group, and so on. You can also use named capture groups.

        >>> df = pl.DataFrame({"x_1": [1], "x_2": [2], "group_id": ["xyz"]})
        >>> df.select(pl.all().name.replace(r"_(\d+)$", ":$1"))
        shape: (1, 3)
        βββββββ¬ββββββ¬βββββββββββ
        β x:1 β x:2 β group_id β
        β --- β --- β ---      β
        β i64 β i64 β str      β
        βββββββͺββββββͺβββββββββββ‘
        β 1   β 2   β xyz      β
        βββββββ΄ββββββ΄βββββββββββ

        The `${1}` form is used to disambiguate the group reference from surrounding
        text.

        >>> df = pl.DataFrame({"hat": [1], "hut": [2]}).with_row_index()
        >>> df.with_columns(pl.all().name.replace(r"^h(.)t", "s$1m"))  # doctest: +SKIP
        # ComputeError: the name 's' passed to `LazyFrame.with_columns` is duplicate

        >>> df.with_columns(pl.all().name.replace(r"^h(.)t", "s${1}m"))
        shape: (1, 5)
        βββββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ
        β index β hat β hut β sam β sum β
        β ---   β --- β --- β --- β --- β
        β u32   β i64 β i64 β i64 β i64 β
        βββββββββͺββββββͺββββββͺββββββͺββββββ‘
        β 0     β 1   β 2   β 1   β 2   β
        βββββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ
        )r   r   Zname_replace)r   r&   r'   r%   r   r   r   Ϊreplace,  s   czExprNameNameSpace.replacec                 C  r   )a%  
        Add a suffix to all field names of a struct.

        Notes
        -----
        This only takes effect for struct columns.

        Parameters
        ----------
        suffix
            Suffix to add to the field name.

        See Also
        --------
        map_fields
        prefix_fields

        Examples
        --------
        >>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
        >>> df.select(pl.col("x").name.suffix_fields("_suffix")).schema
        Schema({'x': Struct({'a_suffix': Int64, 'b_suffix': Int64})})
        )r   r   Zname_suffix_fieldsr   r   r   r   Ϊsuffix_fields  r#   zExprNameNameSpace.suffix_fieldsN)r	   r   r
   r   )r
   r   )r   r   r
   r   )r   r   r
   r   )r   r   r
   r   )r&   r   r'   r   r%   r(   r
   r   )Ϊ__name__Ϊ
__module__Ϊ__qualname__Ϊ__doc__Ϊ	_accessorr   r   r   r   r   r   r!   r"   r$   r)   r*   r   r   r   r   r      s    


-
,
$
$
!
!
er   N)Ϊ
__future__r   Ϊtypingr   Zpolars._utils.wrapr   Ϊcollections.abcr   Zpolarsr   r   r   r   r   r   Ϊ<module>   s    