
    AHj9                        d dl mZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ er:d dlZd d	lmZ d d
lmZ d dlmZ d dlmZmZ ej:                  dk\  rd dlm
Z
 nd dlm
Z
  G d d      Z y)    )annotations)TYPE_CHECKINGLiteraloverloadN)
deprecated)serialize_polars_object)display_dot_graph)	wrap_expr)ComputeError)IOBase)Path)Expr)
SchemaDictSerializationFormat)      c                  @   e Zd ZdZdZd1dZd2dZd2dZd3dZd4dZ	d4dZ
d4d	Zd4d
Zd5dZd5dZd5dZddd6dZddd6dZeddd7d       Zed8d       Zddd9dZddd:dZd;dZd<dZd<dZd=dZe	 d>dd	 	 	 	 	 d?d        Zed>d@d!       Zedd	 	 	 	 	 dAd"       Z	 dBd#d	 	 	 	 	 dCd$Zed>dDd%       ZedEd&       Z ed'      dBdFd(       Zeddd)	 	 	 	 	 dGd*       Zedd	 	 	 	 	 dHd+       Zddd)	 	 	 	 	 dId,Zdddd-dd.	 	 	 	 	 	 	 	 	 	 	 dJd/ZdKd0Zy)LExprMetaNameSpacez*Namespace for expressions on a meta level.metac                &    |j                   | _         y N)_pyexprselfexprs     H/root/tools/cai/cai_env/lib/python3.12/site-packages/polars/expr/meta.py__init__zExprMetaNameSpace.__init__   s    ||    c                N    t        | j                        j                          dS Nz.meta)r
   r   __str__r   s    r   r!   zExprMetaNameSpace.__str__"   s"    DLL)1134E::r   c                N    t        | j                        j                          dS r    )r
   r   __repr__r"   s    r   r$   zExprMetaNameSpace.__repr__%   s"    DLL)2245U;;r   c                6    | j                   j                         S r   )r   __hash__r"   s    r   r&   zExprMetaNameSpace.__hash__(   s    ||$$&&r   c                L    | j                   j                  |j                         S r   r   meta_eqr   others     r   __eq__zExprMetaNameSpace.__eq__+   s    ||##EMM22r   c                    | |k(   S r    r*   s     r   __ne__zExprMetaNameSpace.__ne__.   s    5=  r   c                L    | j                   j                  |j                         S )aX  
        Indicate if this expression is the same as another expression.

        Examples
        --------
        >>> foo_bar = pl.col("foo").alias("bar")
        >>> foo = pl.col("foo")
        >>> foo_bar.meta.eq(foo)
        False
        >>> foo_bar2 = pl.col("foo").alias("bar")
        >>> foo_bar.meta.eq(foo_bar2)
        True
        r(   r*   s     r   eqzExprMetaNameSpace.eq1   s     ||##EMM22r   c                &    | j                  |       S )a\  
        Indicate if this expression is NOT the same as another expression.

        Examples
        --------
        >>> foo_bar = pl.col("foo").alias("bar")
        >>> foo = pl.col("foo")
        >>> foo_bar.meta.ne(foo)
        True
        >>> foo_bar2 = pl.col("foo").alias("bar")
        >>> foo_bar.meta.ne(foo_bar2)
        False
        )r1   r*   s     r   nezExprMetaNameSpace.neA   s     775>!!r   c                6    | j                   j                         S )z
        Indicate if this expression expands into multiple expressions.

        Examples
        --------
        >>> e = pl.col(["a", "b"]).name.suffix("_foo")
        >>> e.meta.has_multiple_outputs()
        True
        )r   meta_has_multiple_outputsr"   s    r   has_multiple_outputsz&ExprMetaNameSpace.has_multiple_outputsQ   s     ||5577r   c                6    | j                   j                         S )aq  
        Indicate if this expression is a basic (non-regex) unaliased column.

        Examples
        --------
        >>> e = pl.col("foo")
        >>> e.meta.is_column()
        True
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.is_column()
        False
        >>> e = pl.col(r"^col.*\d+$")
        >>> e.meta.is_column()
        False
        )r   meta_is_columnr"   s    r   	is_columnzExprMetaNameSpace.is_column]   s      ||**,,r   c                6    | j                   j                         S )z
        Indicate if this expression expands to columns that match a regex pattern.

        Examples
        --------
        >>> e = pl.col("^.*$").name.prefix("foo_")
        >>> e.meta.is_regex_projection()
        True
        )r   meta_is_regex_projectionr"   s    r   is_regex_projectionz%ExprMetaNameSpace.is_regex_projectiono   s     ||4466r   F)allow_aliasingc               8    | j                   j                  |      S )az  
        Indicate if this expression only selects columns (optionally with aliasing).

        This can include bare columns, columns matched by regex or dtype, selectors
        and exclude ops, and (optionally) column/expression aliasing.

        .. versionadded:: 0.20.30

        Parameters
        ----------
        allow_aliasing
            If False (default), any aliasing is not considered to be column selection.
            Set True to allow for column selection that also includes aliasing.

        Examples
        --------
        >>> import polars.selectors as cs
        >>> e = pl.col("foo")
        >>> e.meta.is_column_selection()
        True
        >>> e = pl.col("foo").alias("bar")
        >>> e.meta.is_column_selection()
        False
        >>> e.meta.is_column_selection(allow_aliasing=True)
        True
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.is_column_selection()
        False
        >>> e = cs.starts_with("foo")
        >>> e.meta.is_column_selection()
        True
        >>> e = cs.starts_with("foo").exclude("foo!")
        >>> e.meta.is_column_selection()
        True
        )r   meta_is_column_selectionr   r=   s     r   is_column_selectionz%ExprMetaNameSpace.is_column_selection{   s    H ||44^DDr   c               8    | j                   j                  |      S )a  
        Indicate if this expression is a literal value (optionally aliased).

        .. versionadded:: 1.14

        Parameters
        ----------
        allow_aliasing
            If False (default), only a bare literal will match.
            Set True to also allow for aliased literals.

        Examples
        --------
        >>> from datetime import datetime
        >>> e = pl.lit(123)
        >>> e.meta.is_literal()
        True
        >>> e = pl.lit(987.654321).alias("foo")
        >>> e.meta.is_literal()
        False
        >>> e = pl.lit(datetime.now()).alias("bar")
        >>> e.meta.is_literal(allow_aliasing=True)
        True
        )r   meta_is_literalr@   s     r   
is_literalzExprMetaNameSpace.is_literal   s    2 ||++N;;r   T)raise_if_undeterminedc                    y r   r.   r   rE   s     r   output_namezExprMetaNameSpace.output_name   s    RUr   c                    y r   r.   rG   s     r   rH   zExprMetaNameSpace.output_name   s    SVr   c               \    	 | j                   j                         S # t        $ r |sY y w xY w)aa  
        Get the column name that this expression would produce.

        It may not always be possible to determine the output name as that can depend
        on the schema of the context; in that case this will raise `ComputeError` if
        `raise_if_undetermined` is True (the default), or `None` otherwise.

        Examples
        --------
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.output_name()
        'foo'
        >>> e_filter = pl.col("foo").filter(pl.col("bar") == 13)
        >>> e_filter.meta.output_name()
        'foo'
        >>> e_sum_over = pl.sum("foo").over("groups")
        >>> e_sum_over.meta.output_name()
        'foo'
        >>> e_sum_slice = pl.sum("foo").slice(pl.len() - 10, pl.col("bar"))
        >>> e_sum_slice.meta.output_name()
        'foo'
        >>> pl.len().meta.output_name()
        'len'
        N)r   meta_output_namer   rG   s     r   rH   zExprMetaNameSpace.output_name   s2    2	<<0022 	(	s    ++N)schemac               p    | j                   j                  |      D cg c]  }t        |       c}S c c}w )a<  
        Pop the latest expression and return the input(s) of the popped expression.

        Returns
        -------
        list of Expr
            A list of expressions which in most cases will have a unit length.
            This is not the case when an expression has multiple inputs.
            For instance in a `fold` expression.

        Examples
        --------
        >>> e = pl.col("foo") + pl.col("bar")
        >>> first = e.meta.pop()[0]
        >>> first.meta == pl.col("bar")
        True
        >>> first.meta == pl.col("foo")
        False
        )r   meta_popr
   )r   rL   es      r   popzExprMetaNameSpace.pop   s+    ( '+ll&;&;F&CD	!DDDs   3c                6    | j                   j                         S )aD  
        Get a list with the root column name.

        Examples
        --------
        >>> e = pl.col("foo") * pl.col("bar")
        >>> e.meta.root_names()
        ['foo', 'bar']
        >>> e_filter = pl.col("foo").filter(pl.col("bar") == 13)
        >>> e_filter.meta.root_names()
        ['foo', 'bar']
        >>> e_sum_over = pl.sum("foo").over("groups")
        >>> e_sum_over.meta.root_names()
        ['foo', 'groups']
        >>> e_sum_slice = pl.sum("foo").slice(pl.len() - 10, pl.col("bar"))
        >>> e_sum_slice.meta.root_names()
        ['foo', 'bar']
        )r   meta_root_namesr"   s    r   
root_nameszExprMetaNameSpace.root_names   s    & ||++--r   c                H    t        | j                  j                               S )aR  
        Undo any renaming operation like `alias` or `name.keep`.

        Examples
        --------
        >>> e = pl.col("foo").alias("bar")
        >>> e.meta.undo_aliases().meta == pl.col("foo")
        True
        >>> e = pl.col("foo").sum().over("bar")
        >>> e.name.keep().meta.undo_aliases().meta == e
        True
        )r
   r   meta_undo_aliasesr"   s    r   undo_aliaseszExprMetaNameSpace.undo_aliases  s     779::r   c                ,    t        | j                        S )zReturn the original expression.)r
   r   r"   s    r   as_expressionzExprMetaNameSpace.as_expression  s    &&r   c                p    t         j                  j                  | j                  j	                               S )a2  
        Try to turn this expression in a selector.

        Raises if the underlying expressions is not a column or selector.

        .. warning::
            This functionality is considered **unstable**. It may be changed
            at any point without it being considered a breaking change.
        )plSelector_from_pyselectorr   into_selectorr"   s    r   as_selectorzExprMetaNameSpace.as_selector   s&     {{++DLL,F,F,HIIr   .formatc                    y r   r.   r   filer`   s      r   	serializezExprMetaNameSpace.serialize,  s     r   c                    y r   r.   rb   s      r   rd   zExprMetaNameSpace.serialize1  s    NQr   c                    y r   r.   rb   s      r   rd   zExprMetaNameSpace.serialize4  s     r   binaryc                   |dk(  r| j                   j                  }n,|dk(  r| j                   j                  }nd|}t        |      t	        |||      S )u  
        Serialize this expression to a file or string in JSON format.

        Parameters
        ----------
        file
            File path to which the result should be written. If set to `None`
            (default), the output is returned as a string instead.
        format
            The format in which to serialize. Options:

            - `"binary"`: Serialize to binary format (bytes). This is the default.
            - `"json"`: Serialize to JSON format (string).

        See Also
        --------
        Expr.deserialize

        Notes
        -----
        Serialization is not stable across Polars versions: a LazyFrame serialized
        in one Polars version may not be deserializable in another Polars version.

        Examples
        --------
        Serialize the expression into a binary representation.

        >>> expr = pl.col("foo").sum().over("bar")
        >>> bytes = expr.meta.serialize()
        >>> type(bytes)
        <class 'bytes'>

        The bytes can later be deserialized back into an `Expr` object.

        >>> import io
        >>> pl.Expr.deserialize(io.BytesIO(bytes))
        <Expr ['col("foo").sum().over([col("ba…'] at ...>
        rg   jsonz0`format` must be one of {'binary', 'json'}, got )r   serialize_binaryserialize_json
ValueErrorr   )r   rc   r`   
serializermsgs        r   rd   zExprMetaNameSpace.serialize9  sX    X X66Jv44JFvjQCS/!&z4@@r   c                     y r   r.   r   rc   s     r   
write_jsonzExprMetaNameSpace.write_jsono  s    36r   c                     y r   r.   rp   s     r   rq   zExprMetaNameSpace.write_jsonr  s    =@r   z;`meta.write_json` was renamed; use `meta.serialize` insteadc                (    | j                  |d      S )z
        Write expression to json.

        .. deprecated:: 0.20.11
            This method has been renamed to :meth:`serialize`.
        ri   r_   )rd   rp   s     r   rq   zExprMetaNameSpace.write_jsonu  s     ~~d6~22r   )return_as_stringrL   c                    y r   r.   r   rt   rL   s      r   tree_formatzExprMetaNameSpace.tree_format  s     r   c                    y r   r.   rv   s      r   rw   zExprMetaNameSpace.tree_format  s     r   c               X    | j                   j                  |      }|r|S t        |       y)am  
        Format the expression as a tree.

        Parameters
        ----------
        return_as_string:
            If True, return as string rather than printing to stdout.
        schema
            Optionally provide a schema for the expression tree formatter.
            This is a mapping of column names to their data types. If provided,
            it may be used to enhance the tree formatting with type information.

        Examples
        --------
        >>> e = (pl.col("foo") * pl.col("bar")).sum().over(pl.col("ham")) / 2
        >>> e.meta.tree_format(return_as_string=True)  # doctest: +SKIP
        N)r   meta_tree_formatprint)r   rt   rL   ss       r   rw   zExprMetaNameSpace.tree_format  s*    ( LL))&1H!Hr   )g      0@g      (@)showoutput_path
raw_outputfigsizerL   c               X    | j                   j                  |      }t        |||||      S )a  
        Format the expression as a Graphviz graph.

        Note that Graphviz must be installed to render the visualization (if not
        already present, you can download it here: `<https://graphviz.org/download>`_).

        Parameters
        ----------
        show
            Show the figure.
        output_path
            Write the figure to disk.
        raw_output
            Return dot syntax. This cannot be combined with `show` and/or `output_path`.
        figsize
            Passed to matplotlib if `show == True`.
        schema
            Optionally provide a schema for the expression tree formatter.
            This is a mapping of column names to their data types. If provided,
            it may be used to enhance the tree formatting with type information.

        Examples
        --------
        >>> e = (pl.col("foo") * pl.col("bar")).sum().over(pl.col("ham")) / 2
        >>> e.meta.show_graph()  # doctest: +SKIP
        )dotr}   r~   r   r   )r   meta_show_graphr	   )r   r}   r~   r   r   rL   r   s          r   
show_graphzExprMetaNameSpace.show_graph  s5    F ll**62 #!
 	
r   c                ^    t        | j                  j                  |j                              S r   )r
   r   meta_replace_elementr   s     r   _replace_elementz"ExprMetaNameSpace._replace_element  s     ::4<<HIIr   )r   r   returnNone)r   str)r   int)r+   zExprMetaNameSpace | Exprr   bool)r   r   )r=   r   r   r   )rE   Literal[True]r   r   )rE   Literal[False]r   
str | None)rE   r   r   r   )rL   zSchemaDict | Noner   z
list[Expr])r   z	list[str])r   r   )r   zpl.Selector).)rc   r   r`   zLiteral['binary']r   bytes)rc   r   r`   zLiteral['json']r   r   )rc   IOBase | str | Pathr`   r   r   r   r   )rc   IOBase | str | Path | Noner`   r   r   zbytes | str | None)rc   r   r   r   )rc   r   r   r   )rc   r   r   r   )rt   r   rL   None | SchemaDictr   r   )rt   r   rL   r   r   r   )rt   r   rL   r   r   r   )r}   r   r~   zstr | Path | Noner   r   r   ztuple[float, float]rL   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__	_accessorr   r!   r$   r&   r,   r/   r1   r3   r6   r9   r<   rA   rD   r   rH   rP   rS   rV   rX   r^   rd   rq   r   rw   r   r   r.   r   r   r   r      si   4I$;<'3!3 " 
8-$
7 =B $EL 49 <6 DHU UV V;? @ 26 E,.*;'
J ?B+<	  Q QJM'4G	  ,04A '/	4A(4A $	4A
 
4Al 6 6@ @MN3 O3  ,/$(	 ) "	
 
  NR#0:K	 
 +0T#'9J	< )- '3$(*
 *
 '	*

 *
 %*
 "*
 
*
XJr   r   )!
__future__r   typingr   r   r   polars._reexport	_reexportrZ   polars._utils.deprecationr   polars._utils.serder   polars._utils.variousr	   polars._utils.wrapr
   polars.exceptionsr   sysior   pathlibr   polarsr   polars._typingr   r   version_infowarningstyping_extensionsr   r.   r   r   <module>r      sT    " 3 3  0 7 3 ( *>
7"'0zJ zJr   