
    hZ                        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
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)all_exprs_are_scalar_like)flatten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       dC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\narwhals/group_by.py__init__GroupBy.__init__   8      "
11::JJ~ ; 
    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>Y        ;A""3''    c              3  h   >#    U  H'  u  pUR                  U5      R                  T5      v   M)     g 7fr-   aliasr/   r1   keyvaluer3   s      r%   r4   r5   Z   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_aggsr3   s         @r%   rE   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   rD   )r1   r<   r#   r"   s      r%   r4   #GroupBy.__iter__.<locals>.<genexpr>b   s2      
5	 c]DHH44R895s   58)r    __iter__)r"   s   `r%   rP   GroupBy.__iter__a   s*     
!]]335
 	
 	
s   (313r   r    r   N)r#   r   r$   *Sequence[str] | Sequence[CompliantExprAny]r   boolreturnNone)rG   Expr | Iterable[Expr]rH   r   rU   r   )rU   z Iterator[tuple[Any, DataFrameT]])__name__
__module____qualname____firstlineno__r&   rE   rP   __static_attributes__ r)   r%   r   r      s9    

 9
 
 

<L|
r)   r   c                  8    \ rS rSr        SS jrSS jrSrg)LazyGroupByh   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__i   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.   r0   s     r%   r4   "LazyGroupBy.agg.<locals>.<genexpr>   r6   r7   c              3  h   >#    U  H'  u  pUR                  U5      R                  T5      v   M)     g 7fr-   r9   r;   s      r%   r4   re      r>   r?   r@   rF   s         @r%   rE   LazyGroupBy.aggw   s    p '$-(	()BzBB  (,,hh--/
;;
","2"2"4
 xx''(9(9>(JKKr)   rR   N)r#   r   r$   rS   r   rT   rU   rV   )rG   rW   rH   r   rU   r   )rX   rY   rZ   r[   r&   rE   r\   r]   r)   r%   r_   r_   h   s4    

 9
 
 

ILr)   r_   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   r_   r]   r)   r%   <module>rr      sb    " 7 7 B - 5 &<<;,"\)9:
Q
gj! Q
hXL'*% XLr)   