§
    q-Ph;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dS )é    )ÚannotationsN)ÚTYPE_CHECKING)Úparse_into_list_of_expressions)Úqualified_type_name)Ú	wrap_expr)ÚIterableÚSequence)ÚExpr)ÚIntoExprc                  óN   — 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dS )!ÚExprStructNameSpacez)Namespace for struct related expressions.ÚstructÚexprr
   ÚreturnÚNonec                ó   — |j         | _         d S )N)Ú_pyexpr)Úselfr   s     úR/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/expr/struct.pyÚ__init__zExprStructNameSpace.__init__   s   € Ø”|ˆŒˆˆó    Úitemú	str | intc                ó  — t          |t          ¦  «        r|                      |¦  «        S t          |t          ¦  «        r't	          | 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   r   Úmsgs      r   Ú__getitem__zExprStructNameSpace.__getitem__   s„   € ÝdCÑ Ô ð 	!Ø—:’:˜dÑ#Ô#Ð#Ý˜cÑ"Ô"ð 	!Ý˜Tœ\×?Ò?ÀÑEÔEÑFÔFÐFà\Õ4GÈÑ4MÔ4MÐ\Ð\ÐSWÐ\Ð\Ð\ˆCÝ˜C‘.”.Ð r   Únameústr | list[str]Ú
more_namesr   c                ó  — |rg t          |t          ¦  «        r|gn|¢|¢}t          |t          ¦  «        r't          | j                             |¦  «        ¦  «        S t          | 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   r$   r&   s      r   r   zExprStructNameSpace.field"   s   € ðd ð 	OØN¥
¨4µÑ 5Ô 5Ð?tff¸4ÐNÀ:ÐNˆDÝdDÑ!Ô!ð 	HÝ˜Tœ\×@Ò@ÀÑFÔFÑGÔGÐGå˜œ×:Ò:¸4Ñ@Ô@ÑAÔAÐAr   c                ó,   — |                       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   ÚnamesúSequence[str]c                óP   — t          | 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   r/   s     r   Úrename_fieldsz!ExprStructNameSpace.rename_fieldsÀ   s#   € õ| ˜œ×:Ò:¸5ÑAÔAÑBÔBÐBr   c                óN   — t          | 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   ÚexprsúIntoExpr | Iterable[IntoExpr]Únamed_exprsr   c                óä   — t          t          t          j                             dd¦  «        ¦  «        ¦  «        }t          |i |¤d|i¤Ž}t          | 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   r7   r9   Ú	structifyÚpyexprss        r   Úwith_fieldszExprStructNameSpace.with_fields  sw   € õT RœZŸ^š^Ð,CÀQÑGÔGÑHÔHÑIÔIˆ	å0Øð
Ø!ð
ð 
Ø/8ð
ð 
ð 
ˆõ ˜œ×8Ò8¸ÑAÔAÑBÔBÐBr   N)r   r
   r   r   )r   r   r   r
   )r$   r%   r&   r   r   r
   )r   r
   )r/   r0   r   r
   )r7   r8   r9   r   r   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	_accessorr   r#   r   r.   r3   r6   rD   © r   r   r   r      sÆ   € € € € € Ø3Ð3à€Ið$ð $ð $ð $ð!ð !ð !ð !ðwBð wBð wBð wBðr#ð #ð #ð #ðJ>Cð >Cð >Cð >Cð@<ð <ð <ð <ð*PCð PCð PCð PCð PCð PCr   r   )Ú
__future__r   r>   Útypingr   Úpolars._utils.parser   Úpolars._utils.variousr   Úpolars._utils.wrapr   Úcollections.abcr   r	   Úpolarsr
   Úpolars._typingr   r   rJ   r   r   ú<module>rS      sò   ðØ "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø  Ð  Ð  Ð  Ð  Ð  à >Ð >Ð >Ð >Ð >Ð >Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø (Ð (Ð (Ð (Ð (Ð (àð (Ø2Ð2Ð2Ð2Ð2Ð2Ð2Ð2àÐÐÐÐÐØ'Ð'Ð'Ð'Ð'Ð'ðTCð TCð TCð TCð TCñ TCô TCð TCð TCð TCr   