
    Wh                        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
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)all_exprs_are_scalar_like)flatten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       K/var/www/html/jupyter_env/lib/python3.12/site-packages/narwhals/group_by.py__init__zGroupBy.__init__   <      "
11::JJ~ ; 
    c                B   t        t        |            }t        |i |sd}t        |      | j                  j                         g fd|D        fd|j                         D        }| 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
        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  @   K   | ]  }|j                          y wN_to_compliant_expr.0xplxs     r$   	<genexpr>zGroupBy.agg.<locals>.<genexpr>V        ;Aa""3';   c              3  d   K   | ]'  \  }}|j                  |      j                         ) y wr+   aliasr-   r/   keyvaluer1   s      r$   r2   zGroupBy.agg.<locals>.<genexpr>W   1      C C 33C8   -0
tupler	   r   r   r   __narwhals_namespace__items_with_compliantr   aggr!   aggs
named_aggs	flat_aggsmsgcompliant_aggsr1   s         @r$   rB   zGroupBy.agg#   s    P '$-(	()BzBB  (,,hh--/
;;
","2"2"4
 xx''(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   rA   )r/   r9   r"   r!   s      r$   r2   z#GroupBy.__iter__.<locals>.<genexpr>_   s5      
b c]DHH44R89
s   36)r   __iter__)r!   s   `r$   rK   zGroupBy.__iter__^   s+     
!]]335
 	
 	
s   '202N)r"   r   r#   *Sequence[str] | Sequence[CompliantExprAny]r   boolreturnNone)rD   Expr | Iterable[Expr]rE   r   rN   r   )rN   z Iterator[tuple[Any, DataFrameT]])__name__
__module____qualname__r%   rB   rK    r'   r$   r   r      s9    

 9
 
 

9Lv
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__f   r&   r'   c                B   t        t        |            }t        |i |sd}t        |      | j                  j                         g fd|D        fd|j                         D        }| 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   │|
            |└─────┴─────┴─────┘|
            └───────────────────┘
        r)   c              3  @   K   | ]  }|j                          y wr+   r,   r.   s     r$   r2   z"LazyGroupBy.agg.<locals>.<genexpr>   r3   r4   c              3  d   K   | ]'  \  }}|j                  |      j                         ) y wr+   r6   r8   s      r$   r2   z"LazyGroupBy.agg.<locals>.<genexpr>   r;   r<   r=   rC   s         @r$   rB   zLazyGroupBy.aggt   s    j '$-(	()BzBB  (,,hh--/
;;
","2"2"4
 xx''(9(9(9>(JKKr'   N)r"   r   r#   rL   r   rM   rN   rO   )rD   rP   rE   r   rN   r   )rQ   rR   rS   r%   rB   rT   r'   r$   rV   rV   e   s4    

 9
 
 

FLr'   rV   N)
__future__r   typingr   r   r   r   narwhals._expression_parsingr   narwhals._utilsr	   r
   narwhals.exceptionsr   narwhals.typingr   collections.abcr   r   r   narwhals._compliant.typingr   narwhals.dataframer   narwhals.exprr   r   r   rV   rT   r'   r$   <module>re      sb    " 7 7 B - 5 &<<;,"\)9:
N
gj! N
bUL'*% ULr'   