Ë
    ðmxiè  ã                  óà   — d dl mZ d dlmZ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 erd dlmZmZmZ d dlmZ d d	lmZ d d
lmZ  edd¬«      Z G d„ dee   «      Z G d„ dee   «      Zy)é    )Úannotations)ÚTYPE_CHECKINGÚAnyÚGenericÚTypeVar©Úis_scalar_like)Útupleify)ÚInvalidOperationError)Ú
DataFrameT)ÚIterableÚIteratorÚSequence)ÚCompliantExprAny)Ú	LazyFrame)ÚExprÚ
LazyFrameTzLazyFrame[Any])Úboundc                  ó4   — e Zd Z	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zy)ÚGroupByc              óŒ   — || _         || _        | j                   j                  j                  | j                  |¬«      | _        y ©N)Údrop_null_keys©Ú_dfÚ_keysÚ_compliant_frameÚgroup_byÚ_grouped©ÚselfÚdfÚkeysr   s       úH/home/htdocs/ttos/venv/lib/python3.12/site-packages/narwhals/group_by.pyÚ__init__zGroupBy.__init__   ó<   € ð  "ˆŒØˆŒ
ØŸ™×1Ñ1×:Ñ:Ø�J‰J ~ð ;ó 
ˆ�ó    c                óÜ   —  | j                   j                  |i |¤Ž}t        d„ |D «       «      sd}t        |«      ‚| j                   j	                   | j
                  j                  |Ž «      S )u¿  Compute aggregations for each group of a group by operation.

        Arguments:
            aggs: Aggregations to compute for each group of the group by operation,
                specified as positional arguments.
            named_aggs: Additional aggregations, specified as keyword arguments.

        Examples:
            Group by one column or by multiple columns and call `agg` to compute
            the grouped sum of another column.

            >>> import pandas as pd
            >>> import narwhals as nw
            >>> df_native = pd.DataFrame(
            ...     {
            ...         "a": ["a", "b", "a", "b", "c"],
            ...         "b": [1, 2, 1, 3, 3],
            ...         "c": [5, 4, 3, 2, 1],
            ...     }
            ... )
            >>> df = nw.from_native(df_native)
            >>>
            >>> df.group_by("a").agg(nw.col("b").sum()).sort("a")
            â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”�
            |Narwhals DataFrame|
            |------------------|
            |        a  b      |
            |     0  a  2      |
            |     1  b  5      |
            |     2  c  3      |
            â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜
            >>>
            >>> df.group_by("a", "b").agg(nw.col("c").sum()).sort("a", "b").to_native()
               a  b  c
            0  a  1  8
            1  b  2  4
            2  b  3  2
            3  c  3  1
        c              3  ó2   K  — | ]  }t        |«      –— Œ y ­w©Nr   ©Ú.0Úxs     r$   ú	<genexpr>zGroupBy.agg.<locals>.<genexpr>L   ó   è ø€ Ò=¨”> !×$Ñ=ùó   ‚úÒFound expression which does not aggregate.

All expressions passed to GroupBy.agg must aggregate.
For example, `df.group_by('a').agg(nw.col('b').sum())` is valid,
but `df.group_by('a').agg(nw.col('b'))` is not.©r   Ú_flatten_and_extractÚallr   Ú_with_compliantr   Úagg©r!   ÚaggsÚ
named_aggsÚcompliant_aggsÚmsgs        r$   r6   zGroupBy.agg#   sm   € ðP 7˜Ÿ™×6Ñ6¸ÐKÀ
ÑKˆÜÑ=¨nÔ=Ô=ðBð ô (¨Ó,Ð,Ø�x‰x×'Ñ'Ð(9¨¯©×(9Ñ(9¸>Ð(JÓKÐKr'   c              #  óh   ‡ K  — ˆ fd„‰ j                   j                  «       D «       E d {  –—†  y 7 Œ­w)Nc              3  óp   •K  — | ]-  \  }}t        |«      ‰j                  j                  |«      f–— Œ/ y ­wr*   )r
   r   r5   )r,   Úkeyr"   r!   s      €r$   r.   z#GroupBy.__iter__.<locals>.<genexpr>W   s5   øè ø€ ò 
á��bô �c‹]˜DŸH™H×4Ñ4°RÓ8Ô9ñ
ùs   ƒ36)r   Ú__iter__)r!   s   `r$   r?   zGroupBy.__iter__V   s+   øè ø€ ó
à!Ÿ]™]×3Ñ3Ó5ô
÷ 	
ò 	
ús   ƒ'2ª0«2N)r"   r   r#   ú*Sequence[str] | Sequence[CompliantExprAny]r   ÚboolÚreturnÚNone)r8   úExpr | Iterable[Expr]r9   r   rB   r   )rB   z Iterator[tuple[Any, DataFrameT]])Ú__name__Ú
__module__Ú__qualname__r%   r6   r?   © r'   r$   r   r      s9   „ ð
àð
ð 9ð
ð ð
ð 
ó
ó1Lôf
r'   r   c                  ó,   — e Zd Z	 	 	 	 	 	 	 	 dd„Zdd„Zy)ÚLazyGroupByc              óŒ   — || _         || _        | j                   j                  j                  | j                  |¬«      | _        y r   r   r    s       r$   r%   zLazyGroupBy.__init__^   r&   r'   c                óÜ   —  | j                   j                  |i |¤Ž}t        d„ |D «       «      sd}t        |«      ‚| j                   j	                   | j
                  j                  |Ž «      S )u­  Compute aggregations for each group of a group by operation.

        Arguments:
            aggs: Aggregations to compute for each group of the group by operation,
                specified as positional arguments.
            named_aggs: Additional aggregations, specified as keyword arguments.

        Examples:
            Group by one column or by multiple columns and call `agg` to compute
            the grouped sum of another column.

            >>> import polars as pl
            >>> import narwhals as nw
            >>> from narwhals.typing import IntoFrameT
            >>> lf_native = pl.LazyFrame(
            ...     {
            ...         "a": ["a", "b", "a", "b", "c"],
            ...         "b": [1, 2, 1, 3, 3],
            ...         "c": [5, 4, 3, 2, 1],
            ...     }
            ... )
            >>> lf = nw.from_native(lf_native)
            >>>
            >>> nw.to_native(lf.group_by("a").agg(nw.col("b").sum()).sort("a")).collect()
            shape: (3, 2)
            â”Œâ”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”�
            â”‚ a   â”† b   â”‚
            â”‚ --- â”† --- â”‚
            â”‚ str â”† i64 â”‚
            â•žâ•�â•�â•�â•�â•�â•ªâ•�â•�â•�â•�â•�â•¡
            â”‚ a   â”† 2   â”‚
            â”‚ b   â”† 5   â”‚
            â”‚ c   â”† 3   â”‚
            â””â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”˜
            >>>
            >>> lf.group_by("a", "b").agg(nw.sum("c")).sort("a", "b").collect()
            â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”�
            |Narwhals DataFrame |
            |-------------------|
            |shape: (4, 3)      |
            |â”Œâ”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”�|
            |â”‚ a   â”† b   â”† c   â”‚|
            |â”‚ --- â”† --- â”† --- â”‚|
            |â”‚ str â”† i64 â”† i64 â”‚|
            |â•žâ•�â•�â•�â•�â•�â•ªâ•�â•�â•�â•�â•�â•ªâ•�â•�â•�â•�â•�â•¡|
            |â”‚ a   â”† 1   â”† 8   â”‚|
            |â”‚ b   â”† 2   â”† 4   â”‚|
            |â”‚ b   â”† 3   â”† 2   â”‚|
            |â”‚ c   â”† 3   â”† 1   â”‚|
            |â””â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”´â”€â”€â”€â”€â”€â”˜|
            â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜
        c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wr*   r   r+   s     r$   r.   z"LazyGroupBy.agg.<locals>.<genexpr>¢   r/   r0   r1   r2   r7   s        r$   r6   zLazyGroupBy.aggl   sm   € ðj 7˜Ÿ™×6Ñ6¸ÐKÀ
ÑKˆÜÑ=¨nÔ=Ô=ðBð ô (¨Ó,Ð,Ø�x‰x×'Ñ'Ð(9¨¯©×(9Ñ(9¸>Ð(JÓKÐKr'   N)r"   r   r#   r@   r   rA   rB   rC   )r8   rD   r9   r   rB   r   )rE   rF   rG   r%   r6   rH   r'   r$   rJ   rJ   ]   s3   „ ð
àð
ð 9ð
ð ð
ð 
ó
ô>Lr'   rJ   N)Ú
__future__r   Útypingr   r   r   r   Únarwhals._expression_parsingr	   Únarwhals._utilsr
   Únarwhals.exceptionsr   Únarwhals.typingr   Úcollections.abcr   r   r   Únarwhals._compliant.typingr   Únarwhals.dataframer   Únarwhals.exprr   r   r   rJ   rH   r'   r$   ú<module>rX      sb   ðÝ "ç 7Ó 7å 7Ý $Ý 5Ý &áß<Ñ<å;Ý,Ý"á�\Ð)9Ô:€
ôF
ˆg�jÑ!ô F
ôRML�'˜*Ñ%õ MLr'   