
    Lpj                        S SK Jr  S SKJrJrJr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JrJr  S SKJr  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TypeVaris_scalar_like)tupleify)InvalidOperationError)
DataFrameT)IterableIteratorSequence)CompliantExprAny)	LazyFrame)Expr
LazyFrameTzLazyFrame[Any])boundc                  B    \ rS rSr        SS jrSS jrS	S jrSrg)
GroupBy   c                  Xl         X l        U R                   R                  R                  U R                  US9U l        g N)drop_null_keys_df_keys_compliant_framegroup_by_groupedselfdfkeysr   s       M/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/narwhals/group_by.py__init__GroupBy.__init__   8      "
11::JJ~ ; 
    c                    U R                   R                  " U0 UD6n[        S U 5       5      (       d  Sn[        U5      e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.

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  8   #    U  H  n[        U5      v   M     g 7fNr   .0xs     r%   	<genexpr>GroupBy.agg.<locals>.<genexpr>L        =n>!$$n   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%   r9   GroupBy.agg#   sk    P 66K
K=n===B  (,,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   r8   )r.   keyr#   r"   s      r%   r0   #GroupBy.__iter__.<locals>.<genexpr>W   s2      
5	 c]DHH44R895s   58)r    __iter__)r"   s   `r%   rD   GroupBy.__iter__V   s*     
!]]335
 	
 	
s   (313r   r    r   N)r#   r   r$   *Sequence[str] | Sequence[CompliantExprAny]r   boolreturnNone)r;   Expr | Iterable[Expr]r<   r   rI   r   )rI   z Iterator[tuple[Any, DataFrameT]])__name__
__module____qualname____firstlineno__r&   r9   rD   __static_attributes__ r)   r%   r   r      s9    

 9
 
 

1Lf
r)   r   c                  8    \ rS rSr        SS jrSS jrSrg)LazyGroupBy]   c                  Xl         X l        U R                   R                  R                  U R                  US9U l        g r   r   r!   s       r%   r&   LazyGroupBy.__init__^   r(   r)   c                    U R                   R                  " U0 UD6n[        S U 5       5      (       d  Sn[        U5      e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.

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  8   #    U  H  n[        U5      v   M     g 7fr,   r   r-   s     r%   r0   "LazyGroupBy.agg.<locals>.<genexpr>   r2   r3   r4   r5   r:   s        r%   r9   LazyGroupBy.aggl   sk    j 66K
K=n===B  (,,xx''(9(9>(JKKr)   rF   N)r#   r   r$   rG   r   rH   rI   rJ   )r;   rK   r<   r   rI   r   )rL   rM   rN   rO   r&   r9   rP   rQ   r)   r%   rS   rS   ]   s3    

 9
 
 

>Lr)   rS   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   rS   rQ   r)   r%   <module>re      sb    " 7 7 7 $ 5 &<<;,"\)9:
F
gj! F
RML'*% MLr)   