
    -Ph:                        d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ er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dS )    )annotations)TYPE_CHECKINGAnyGenericIterableIteratorSequenceTypeVar)all_exprs_are_scalar_like)flattentupleify)InvalidOperationError)
DataFrameT)CompliantExprAny)	LazyFrame)Expr
LazyFrameTzLazyFrame[Any])boundc                  &    e Zd Zdd	ZddZddZdS )GroupBydfr   keys*Sequence[str] | Sequence[CompliantExprAny]drop_null_keysboolreturnNonec              x    || _         || _        | j         j                            | j        |          | _        d S N)r   _df_keys_compliant_framegroup_by_groupedselfr   r   r   s       Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/narwhals/group_by.py__init__zGroupBy.__init__   >      "
1::J~ ; 
 
    aggsExpr | Iterable[Expr]
named_aggsr   c                X   t          t          |                    }t          |i |sd}t          |          | j                                        g fd|D             fd|                                D             R }| 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.

        Returns:
            A new Dataframe.

        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
        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.c              3  B   K   | ]}|                               V  d S N_to_compliant_expr.0xplxs     r(   	<genexpr>zGroupBy.agg.<locals>.<genexpr>W   1      ;;Aa""3'';;;;;;r+   c              3  n   K   | ]/\  }}|                     |                                        V  0d S r2   aliasr4   r6   keyvaluer8   s      r(   r9   zGroupBy.agg.<locals>.<genexpr>X   U        C C  33C88     r+   
tupler   r   r   r!   __narwhals_namespace__items_with_compliantr%   aggr'   r,   r.   	flat_aggsmsgcompliant_aggsr8   s         @r(   rG   zGroupBy.agg!   s    V '$--((	()BzBB 	-B  (,,,h--//
;;;;;;;
   ","2"2"4"4  
 
 x''(9(9>(JKKKr+    Iterator[tuple[Any, DataFrameT]]c              #  b    K    fd j                                         D             E d {V  d S )Nc              3  p   K   | ]0\  }}t          |          j                            |          fV  1d S r2   )r   r!   rF   )r6   r?   r   r'   s      r(   r9   z#GroupBy.__iter__.<locals>.<genexpr>`   sV       
 
b c]]DH44R889
 
 
 
 
 
r+   )r%   __iter__)r'   s   `r(   rO   zGroupBy.__iter___   sh      
 
 
 
!]3355
 
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r+   N)r   r   r   r   r   r   r   r   )r,   r-   r.   r   r   r   )r   rL   )__name__
__module____qualname__r)   rG   rO    r+   r(   r   r      sU        
 
 
 
<L <L <L <L|
 
 
 
 
 
r+   r   c                      e Zd Zdd	ZddZdS )LazyGroupByr   r   r   r   r   r   r   r   c              x    || _         || _        | j         j                            | j        |          | _        d S r   r    r&   s       r(   r)   zLazyGroupBy.__init__g   r*   r+   r,   r-   r.   r   c                X   t          t          |                    }t          |i |sd}t          |          | j                                        g fd|D             fd|                                D             R }| 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.

        Returns:
            A new LazyFrame.

        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   │|
            |└─────┴─────┴─────┘|
            └───────────────────┘
        r0   c              3  B   K   | ]}|                               V  d S r2   r3   r5   s     r(   r9   z"LazyGroupBy.agg.<locals>.<genexpr>   r:   r+   c              3  n   K   | ]/\  }}|                     |                                        V  0d S r2   r<   r>   s      r(   r9   z"LazyGroupBy.agg.<locals>.<genexpr>   rA   r+   rB   rH   s         @r(   rG   zLazyGroupBy.aggu   s    p '$--((	()BzBB 	-B  (,,,h--//
;;;;;;;
   ","2"2"4"4  
 
 x''(9(9>(JKKKr+   N)r   r   r   r   r   r   r   r   )r,   r-   r.   r   r   r   )rP   rQ   rR   r)   rG   rS   r+   r(   rU   rU   f   sH        
 
 
 
IL IL IL IL IL ILr+   rU   N)
__future__r   typingr   r   r   r   r   r	   r
   narwhals._expression_parsingr   narwhals._utilsr   r   narwhals.exceptionsr   narwhals.typingr   narwhals._compliant.typingr   narwhals.dataframer   narwhals.exprr   r   r   rU   rS   r+   r(   <module>rc      ss   " " " " " " U U U U U U U U U U U U U U U U U U B B B B B B - - - - - - - - 5 5 5 5 5 5 & & & & & & #;;;;;;,,,,,,""""""W\)9:::
Q
 Q
 Q
 Q
 Q
gj! Q
 Q
 Q
hXL XL XL XL XL'*% XL XL XL XL XLr+   