Λ
    θΛh;6  γ                  σ    d dl mZ d dl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mZmZ d dlmZ d d	lmZ  G d
 d«      Zy)ι    )ΪannotationsN)ΪTYPE_CHECKING)Ϊparse_into_list_of_expressions)Ϊqualified_type_name)Ϊ	wrap_expr)ΪIterableΪSequence)ΪExpr)ΪIntoExprc                  σX    e Zd ZdZdZddZddZddZddZddZ	ddZ
	 	 	 	 	 	 dd	Zy
)ΪExprStructNameSpacez)Namespace for struct related expressions.Ϊstructc                σ&    |j                   | _         y )N)Ϊ_pyexpr)ΪselfΪexprs     ϊn/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/polars/expr/struct.pyΪ__init__zExprStructNameSpace.__init__   s    Ψ||σ    c                σζ    t        |t        «      r| j                  |«      S t        |t        «      r$t	        | j
                  j                  |«      «      S dt        |«      d|d}t        |«      )Nzexpected type 'int | str', got z (ϊ))	Ϊ
isinstanceΪstrΪfieldΪintr   r   Ϊstruct_field_by_indexr   Ϊ	TypeError)r   ΪitemΪmsgs      r   Ϊ__getitem__zExprStructNameSpace.__getitem__   sh    άdCΤ Ψ::dΣ#Π#άcΤ"άT\\Χ?Ρ?ΐΣEΣFΠFΰ3Τ4GΘΣ4MΠ3PΠPRΠSWΠRZΠZ[Π\CάC.Π r   c                σθ    |rg t        |t        «      r|gn|’|’}t        |t        «      r$t        | j                  j                  |«      «      S t        | j                  j                  |«      «      S )u  
        Retrieve one or multiple `Struct` field(s) as a new Series.

        Parameters
        ----------
        name
            Name of the struct field to retrieve.
        *more_names
            Additional struct field names.

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "aaa": [1, 2],
        ...         "bbb": ["ab", "cd"],
        ...         "ccc": [True, None],
        ...         "ddd": [[1, 2], [3]],
        ...     }
        ... ).select(pl.struct("aaa", "bbb", "ccc", "ddd").alias("struct_col"))
        >>> df
        shape: (2, 1)
        ββββββββββββββββββββββββ
        β struct_col           β
        β ---                  β
        β struct[4]            β
        ββββββββββββββββββββββββ‘
        β {1,"ab",true,[1, 2]} β
        β {2,"cd",null,[3]}    β
        ββββββββββββββββββββββββ

        Retrieve struct field(s) as Series:

        >>> df.select(pl.col("struct_col").struct.field("bbb"))
        shape: (2, 1)
        βββββββ
        β bbb β
        β --- β
        β str β
        βββββββ‘
        β ab  β
        β cd  β
        βββββββ

        >>> df.select(
        ...     pl.col("struct_col").struct.field("bbb"),
        ...     pl.col("struct_col").struct.field("ddd"),
        ... )
        shape: (2, 2)
        βββββββ¬ββββββββββββ
        β bbb β ddd       β
        β --- β ---       β
        β str β list[i64] β
        βββββββͺββββββββββββ‘
        β ab  β [1, 2]    β
        β cd  β [3]       β
        βββββββ΄ββββββββββββ

        Use wildcard expansion:

        >>> df.select(pl.col("struct_col").struct.field("*"))
        shape: (2, 4)
        βββββββ¬ββββββ¬βββββββ¬ββββββββββββ
        β aaa β bbb β ccc  β ddd       β
        β --- β --- β ---  β ---       β
        β i64 β str β bool β list[i64] β
        βββββββͺββββββͺβββββββͺββββββββββββ‘
        β 1   β ab  β true β [1, 2]    β
        β 2   β cd  β null β [3]       β
        βββββββ΄ββββββ΄βββββββ΄ββββββββββββ

        Retrieve multiple fields by name:

        >>> df.select(pl.col("struct_col").struct.field("aaa", "bbb"))
        shape: (2, 2)
        βββββββ¬ββββββ
        β aaa β bbb β
        β --- β --- β
        β i64 β str β
        βββββββͺββββββ‘
        β 1   β ab  β
        β 2   β cd  β
        βββββββ΄ββββββ

        Retrieve multiple fields by regex expansion:

        >>> df.select(pl.col("struct_col").struct.field("^a.*|b.*$"))
        shape: (2, 2)
        βββββββ¬ββββββ
        β aaa β bbb β
        β --- β --- β
        β i64 β str β
        βββββββͺββββββ‘
        β 1   β ab  β
        β 2   β cd  β
        βββββββ΄ββββββ

        Notes
        -----
        The `struct` namespace has implemented `__getitem__`
        so you can also access fields by index:

        >>> df.select(pl.col("struct_col").struct[1])
        shape: (2, 1)
        βββββββ
        β bbb β
        β --- β
        β str β
        βββββββ‘
        β ab  β
        β cd  β
        βββββββ
        )r   r   Ϊlistr   r   Ϊstruct_multiple_fieldsΪstruct_field_by_name)r   ΪnameΪ
more_namess      r   r   zExprStructNameSpace.field"   sb    ρd ΨN€
¨4΄Τ 5tfΈ4ΠNΐ:ΠNDάdDΤ!άT\\Χ@Ρ@ΐΣFΣGΠGδΧ:Ρ:Έ4Σ@ΣAΠAr   c                σ$    | j                  d«      S )uό  
        Expand the struct into its individual fields.

        Alias for `Expr.struct.field("*")`.

        >>> df = pl.DataFrame(
        ...     {
        ...         "aaa": [1, 2],
        ...         "bbb": ["ab", "cd"],
        ...         "ccc": [True, None],
        ...         "ddd": [[1, 2], [3]],
        ...     }
        ... ).select(pl.struct("aaa", "bbb", "ccc", "ddd").alias("struct_col"))
        >>> df
        shape: (2, 1)
        ββββββββββββββββββββββββ
        β struct_col           β
        β ---                  β
        β struct[4]            β
        ββββββββββββββββββββββββ‘
        β {1,"ab",true,[1, 2]} β
        β {2,"cd",null,[3]}    β
        ββββββββββββββββββββββββ
        >>> df.select(pl.col("struct_col").struct.unnest())
        shape: (2, 4)
        βββββββ¬ββββββ¬βββββββ¬ββββββββββββ
        β aaa β bbb β ccc  β ddd       β
        β --- β --- β ---  β ---       β
        β i64 β str β bool β list[i64] β
        βββββββͺββββββͺβββββββͺββββββββββββ‘
        β 1   β ab  β true β [1, 2]    β
        β 2   β cd  β null β [3]       β
        βββββββ΄ββββββ΄βββββββ΄ββββββββββββ
        Ϊ*)r   ©r   s    r   ΪunnestzExprStructNameSpace.unnest   s    πF zz#Πr   c                σJ    t        | j                  j                  |«      «      S )u)
  
        Rename the fields of the struct.

        Parameters
        ----------
        names
            New names, given in the same order as the struct's fields.

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "aaa": [1, 2],
        ...         "bbb": ["ab", "cd"],
        ...         "ccc": [True, None],
        ...         "ddd": [[1, 2], [3]],
        ...     }
        ... ).select(pl.struct("aaa", "bbb", "ccc", "ddd").alias("struct_col"))
        >>> df
        shape: (2, 1)
        ββββββββββββββββββββββββ
        β struct_col           β
        β ---                  β
        β struct[4]            β
        ββββββββββββββββββββββββ‘
        β {1,"ab",true,[1, 2]} β
        β {2,"cd",null,[3]}    β
        ββββββββββββββββββββββββ

        >>> df.unnest("struct_col")
        shape: (2, 4)
        βββββββ¬ββββββ¬βββββββ¬ββββββββββββ
        β aaa β bbb β ccc  β ddd       β
        β --- β --- β ---  β ---       β
        β i64 β str β bool β list[i64] β
        βββββββͺββββββͺβββββββͺββββββββββββ‘
        β 1   β ab  β true β [1, 2]    β
        β 2   β cd  β null β [3]       β
        βββββββ΄ββββββ΄βββββββ΄ββββββββββββ

        Rename fields:

        >>> df = df.select(
        ...     pl.col("struct_col").struct.rename_fields(["www", "xxx", "yyy", "zzz"])
        ... )
        >>> df.unnest("struct_col")
        shape: (2, 4)
        βββββββ¬ββββββ¬βββββββ¬ββββββββββββ
        β www β xxx β yyy  β zzz       β
        β --- β --- β ---  β ---       β
        β i64 β str β bool β list[i64] β
        βββββββͺββββββͺβββββββͺββββββββββββ‘
        β 1   β ab  β true β [1, 2]    β
        β 2   β cd  β null β [3]       β
        βββββββ΄ββββββ΄βββββββ΄ββββββββββββ

        Following a rename, the previous field names (obviously) cannot be referenced:

        >>> df.select(pl.col("struct_col").struct.field("aaa"))  # doctest: +SKIP
        StructFieldNotFoundError: aaa
        )r   r   Ϊstruct_rename_fields)r   Ϊnamess     r   Ϊrename_fieldsz!ExprStructNameSpace.rename_fieldsΐ   s    τ| Χ:Ρ:Έ5ΣAΣBΠBr   c                σH    t        | j                  j                  «       «      S )u  
        Convert this struct to a string column with json values.

        Examples
        --------
        >>> pl.DataFrame(
        ...     {"a": [{"a": [1, 2], "b": [45]}, {"a": [9, 1, 3], "b": None}]}
        ... ).with_columns(pl.col("a").struct.json_encode().alias("encoded"))
        shape: (2, 2)
        ββββββββββββββββββββ¬βββββββββββββββββββββββββ
        β a                β encoded                β
        β ---              β ---                    β
        β struct[2]        β str                    β
        ββββββββββββββββββββͺβββββββββββββββββββββββββ‘
        β {[1, 2],[45]}    β {"a":[1,2],"b":[45]}   β
        β {[9, 1, 3],null} β {"a":[9,1,3],"b":null} β
        ββββββββββββββββββββ΄βββββββββββββββββββββββββ
        )r   r   Ϊstruct_json_encoder)   s    r   Ϊjson_encodezExprStructNameSpace.json_encode   s    τ& Χ8Ρ8Σ:Σ;Π;r   c                σΜ    t        t        t        j                  j	                  dd«      «      «      }t        |i |€d|i€}t        | j                  j                  |«      «      S )uό
  
        Add or overwrite fields of this struct.

        This is similar to `with_columns` on `DataFrame`.

        .. versionadded:: 0.20.27

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "coords": [{"x": 1, "y": 4}, {"x": 4, "y": 9}, {"x": 9, "y": 16}],
        ...         "multiply": [10, 2, 3],
        ...     }
        ... )
        >>> df
        shape: (3, 2)
        βββββββββββββ¬βββββββββββ
        β coords    β multiply β
        β ---       β ---      β
        β struct[2] β i64      β
        βββββββββββββͺβββββββββββ‘
        β {1,4}     β 10       β
        β {4,9}     β 2        β
        β {9,16}    β 3        β
        βββββββββββββ΄βββββββββββ
        >>> df = df.with_columns(
        ...     pl.col("coords").struct.with_fields(
        ...         pl.field("x").sqrt(),
        ...         y_mul=pl.field("y") * pl.col("multiply"),
        ...     )
        ... )
        >>> df
        shape: (3, 2)
        βββββββββββββββ¬βββββββββββ
        β coords      β multiply β
        β ---         β ---      β
        β struct[3]   β i64      β
        βββββββββββββββͺβββββββββββ‘
        β {1.0,4,40}  β 10       β
        β {2.0,9,18}  β 2        β
        β {3.0,16,48} β 3        β
        βββββββββββββββ΄βββββββββββ
        >>> df.unnest("coords")
        shape: (3, 4)
        βββββββ¬ββββββ¬ββββββββ¬βββββββββββ
        β x   β y   β y_mul β multiply β
        β --- β --- β ---   β ---      β
        β f64 β i64 β i64   β i64      β
        βββββββͺββββββͺββββββββͺβββββββββββ‘
        β 1.0 β 4   β 40    β 10       β
        β 2.0 β 9   β 18    β 2        β
        β 3.0 β 16  β 48    β 3        β
        βββββββ΄ββββββ΄ββββββββ΄βββββββββββ

        Parameters
        ----------
        *exprs
            Field(s) to add, specified as positional arguments.
            Accepts expression input. Strings are parsed as column names, other
            non-expression inputs are parsed as literals.
        **named_exprs
            Additional fields to add, specified as keyword arguments.
            The columns will be renamed to the keyword used.

        See Also
        --------
        field
        ΪPOLARS_AUTO_STRUCTIFYr   Ϊ__structify)	Ϊboolr   ΪosΪenvironΪgetr   r   r   Ϊstruct_with_fields)r   ΪexprsΪnamed_exprsΪ	structifyΪpyexprss        r   Ϊwith_fieldszExprStructNameSpace.with_fields  s]    τT RZZ^^Π,CΐQΣGΣHΣI	δ0Ψπ
Ψ!ρ
Ψ/8ς
τ Χ8Ρ8ΈΣAΣBΠBr   N)r   r
   ΪreturnΪNone)r   z	str | intr?   r
   )r%   zstr | list[str]r&   r   r?   r
   )r?   r
   )r-   zSequence[str]r?   r
   )r:   zIntoExpr | Iterable[IntoExpr]r;   r   r?   r
   )Ϊ__name__Ϊ
__module__Ϊ__qualname__Ϊ__doc__Ϊ	_accessorr   r    r   r*   r.   r1   r>   © r   r   r   r      sW    Ω3ΰIσ$σ!σwBσr#σJ>Cσ@<π*PCΰ-πPCπ  πPCπ 
τ	PCr   r   )Ϊ
__future__r   r6   Ϊtypingr   Ϊpolars._utils.parser   Ϊpolars._utils.variousr   Ϊpolars._utils.wrapr   Ϊcollections.abcr   r	   Ϊpolarsr
   Ϊpolars._typingr   r   rF   r   r   ϊ<module>rO      s1   πέ "γ 	έ  ε >έ 5έ (αί2εέ'χTCς TCr   