
    }i                        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
 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dS )    )annotations)TYPE_CHECKINGAnyGenericTypeVaris_scalar_like)tupleify)InvalidOperationError)
DataFrameT)IterableIteratorSequence)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       E/home/jrussi/.local/lib/python3.11/site-packages/narwhals/group_by.py__init__zGroupBy.__init__   >      "
1::J~ ; 
 
    aggsExpr | Iterable[Expr]
named_aggsr   c                     | j         j        |i |}t          d |D                       sd}t          |          | 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
        c              3  4   K   | ]}t          |          V  d S Nr   .0xs     r(   	<genexpr>zGroupBy.agg.<locals>.<genexpr>L   *      ==>!$$======r+   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'   r,   r.   compliant_aggsmsgs        r(   r<   zGroupBy.agg#   s{    P 76K
KK==n===== 	-B  (,,,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 r1   )r
   r!   r;   )r3   keyr   r'   s      r(   r5   z#GroupBy.__iter__.<locals>.<genexpr>W   sV       
 
b c]]DH44R889
 
 
 
 
 
r+   )r%   __iter__)r'   s   `r(   rD   zGroupBy.__iter__V   sh      
 
 
 
!]3355
 
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r+   N)r   r   r   r   r   r   r   r   )r,   r-   r.   r   r   r   )r   r@   )__name__
__module____qualname__r)   r<   rD    r+   r(   r   r      sU        
 
 
 
1L 1L 1L 1Lf
 
 
 
 
 
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__^   r*   r+   r,   r-   r.   r   c                     | j         j        |i |}t          d |D                       sd}t          |          | 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   │|
            |└─────┴─────┴─────┘|
            └───────────────────┘
        c              3  4   K   | ]}t          |          V  d S r1   r   r2   s     r(   r5   z"LazyGroupBy.agg.<locals>.<genexpr>   r6   r+   r7   r8   r=   s        r(   r<   zLazyGroupBy.aggl   s{    j 76K
KK==n===== 	-B  (,,,x''(9(9>(JKKKr+   N)r   r   r   r   r   r   r   r   )r,   r-   r.   r   r   r   )rE   rF   rG   r)   r<   rH   r+   r(   rJ   rJ   ]   sB        
 
 
 
>L >L >L >L >L >Lr+   rJ   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   rJ   rH   r+   r(   <module>rX      sy   " " " " " " 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 $ $ $ $ $ $ 5 5 5 5 5 5 & & & & & & #<<<<<<<<<<;;;;;;,,,,,,""""""W\)9:::
F
 F
 F
 F
 F
gj! F
 F
 F
RML ML ML ML ML'*% ML ML ML ML MLr+   