
    h                        S SK Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S S	KJr  S S
KJr  S SKJr  S SKJr  \(       a  S SKJr  S SKJr  \" SSS9r " S S\\   5      r " S S\\   5      rg)    )annotations)TYPE_CHECKING)Any)Generic)Iterable)Iterator)TypeVar)all_exprs_are_scalar_like)InvalidOperationError)
DataFrameT)flatten)tupleify)	LazyFrame)Expr
LazyFrameTzLazyFrame[Any])boundc                  2    \ rS rSrSS jrSS jrS	S jrSrg)
GroupBy   c                   Xl         X0l        U R                   R                  R                  " U R                  SU06U l        g Ndrop_null_keys_df_keys_compliant_framegroup_by_groupedselfdfr   keyss       C/var/www/html/env/lib/python3.13/site-packages/narwhals/group_by.py__init__GroupBy.__init__   7    !
11::ZZ
(6
    c                P  ^ [        [        U5      5      n[        U0 UD6(       d  Sn[        U5      eU R                  R                  5       m/ U4S jU 5       QU4S jUR                  5        5       Q7nU R                  R                  U R                  R                  " U6 5      $ )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  D   >#    U  H  oR                  T5      v   M     g 7fN_to_compliant_expr.0xplxs     r#   	<genexpr>GroupBy.agg.<locals>.<genexpr>U        ;A""3''    c              3  h   >#    U  H'  u  pUR                  U5      R                  T5      v   M)     g 7fr+   aliasr-   r/   keyvaluer1   s      r#   r2   r3   V   1      "4JC C 33C88"4   /2
tupler   r
   r   r   __narwhals_namespace__items_with_compliantr   aggr    aggs
named_aggs	flat_aggsmsgcompliant_aggsr1   s         @r#   rC   GroupBy.agg   s    V '$-(	()BzBB  (,,hh--/
;;
","2"2"4
 xx''(9(9>(JKKr'   c              #  j   ^ #    U 4S jT R                   R                  5        5        S h  vN   g  N7f)Nc              3  t   >#    U  H-  u  p[        U5      TR                  R                  U5      4v   M/     g 7fr+   )r   r   rB   )r/   r:   r!   r    s      r#   r2   #GroupBy.__iter__.<locals>.<genexpr>^   s2      
5	 c]DHH44R895s   58)r   __iter__)r    s   `r#   rN   GroupBy.__iter__]   s*     
!]]335
 	
 	
s   (313r   r   r   N)r!   r   r"   strr   boolreturnNone)rE   Expr | Iterable[Expr]rF   r   rS   r   )rS   z Iterator[tuple[Any, DataFrameT]])__name__
__module____qualname____firstlineno__r$   rC   rN   __static_attributes__ r'   r#   r   r      s    
<L|
r'   r   c                  (    \ rS rSrSS jrSS jrSrg)LazyGroupByd   c                   Xl         X0l        U R                   R                  R                  " U R                  SU06U l        g r   r   r   s       r#   r$   LazyGroupBy.__init__e   r&   r'   c                P  ^ [        [        U5      5      n[        U0 UD6(       d  Sn[        U5      eU R                  R                  5       m/ U4S jU 5       QU4S jUR                  5        5       Q7nU R                  R                  U R                  R                  " U6 5      $ )uL  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   │|
    |└─────┴─────┴─────┘|
    └───────────────────┘
r)   c              3  D   >#    U  H  oR                  T5      v   M     g 7fr+   r,   r.   s     r#   r2   "LazyGroupBy.agg.<locals>.<genexpr>   r4   r5   c              3  h   >#    U  H'  u  pUR                  U5      R                  T5      v   M)     g 7fr+   r7   r9   s      r#   r2   rc      r<   r=   r>   rD   s         @r#   rC   LazyGroupBy.aggl   s    p '$-(	()BzBB  (,,hh--/
;;
","2"2"4
 xx''(9(9>(JKKr'   rP   N)r!   r   r"   rQ   r   rR   rS   rT   )rE   rU   rF   r   rS   r   )rV   rW   rX   rY   r$   rC   rZ   r[   r'   r#   r]   r]   d   s    
ILr'   r]   N)
__future__r   typingr   r   r   r   r   r	   narwhals._expression_parsingr
   narwhals.exceptionsr   narwhals.typingr   narwhals.utilsr   r   narwhals.dataframer   narwhals.exprr   r   r   r]   r[   r'   r#   <module>rn      sh    "        B 5 & " #,"\)9:
J
gj! J
ZQL'*% QLr'   