Λ
    Τͺg  γ                  σπ    d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl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  ed«      Z ed«      Z G d dee   «      Z G d dee   «      Zy)ι    )Ϊannotations)ΪTYPE_CHECKING)ΪAny)ΪGeneric)ΪIterable)ΪIterator)ΪTypeVar)Ϊcast)Ϊ	DataFrame)Ϊ	LazyFrame)Ϊtupleify)ΪIntoExprΪ
DataFrameTΪ
LazyFrameTc                  σ0    e Zd ZddZ	 	 	 	 	 	 ddZddZy)ΪGroupByc               σ΄    t        t        t           |«      | _        || _         | j                  j
                  j                  | j                  d|i| _        y ©NΪdrop_null_keys)r
   r   r   Ϊ_dfΪ_keysΪ_compliant_frameΪgroup_byΪ_grouped©ΪselfΪdfr   Ϊkeyss       ϊF/var/www/openai/venv/lib/python3.12/site-packages/narwhals/group_by.pyΪ__init__zGroupBy.__init__   σH    ά	€#¨Σ+Ψ
Ψ:Χ1Ρ1Χ:Ρ:ΨZZπ
Ψ(6ρ
σ    c                σͺ     | j                   j                  |i |€\  }}| j                   j                   | j                  j                  |i |€«      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.

        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 polars as pl
            >>> import narwhals as nw
            >>> df_pd = pd.DataFrame(
            ...     {
            ...         "a": ["a", "b", "a", "b", "c"],
            ...         "b": [1, 2, 1, 3, 3],
            ...         "c": [5, 4, 3, 2, 1],
            ...     }
            ... )
            >>> df_pl = pl.DataFrame(
            ...     {
            ...         "a": ["a", "b", "a", "b", "c"],
            ...         "b": [1, 2, 1, 3, 3],
            ...         "c": [5, 4, 3, 2, 1],
            ...     }
            ... )

            We define library agnostic functions:

            >>> @nw.narwhalify
            ... def func(df):
            ...     return df.group_by("a").agg(nw.col("b").sum()).sort("a")

            >>> @nw.narwhalify
            ... def func_mult_col(df):
            ...     return df.group_by("a", "b").agg(nw.sum("c")).sort("a", "b")

            We can then pass either pandas or Polars to `func` and `func_mult_col`:

            >>> func(df_pd)
               a  b
            0  a  2
            1  b  5
            2  c  3
            >>> func(df_pl)
            shape: (3, 2)
            βββββββ¬ββββββ
            β a   β b   β
            β --- β --- β
            β str β i64 β
            βββββββͺββββββ‘
            β a   β 2   β
            β b   β 5   β
            β c   β 3   β
            βββββββ΄ββββββ
            >>> func_mult_col(df_pd)
               a  b  c
            0  a  1  8
            1  b  2  4
            2  b  3  2
            3  c  3  1
            >>> func_mult_col(df_pl)
            shape: (4, 3)
            βββββββ¬ββββββ¬ββββββ
            β a   β b   β c   β
            β --- β --- β --- β
            β str β i64 β i64 β
            βββββββͺββββββͺββββββ‘
            β a   β 1   β 8   β
            β b   β 2   β 4   β
            β b   β 3   β 2   β
            β c   β 3   β 1   β
            βββββββ΄ββββββ΄ββββββ
        ©r   Ϊ_flatten_and_extractΪ_from_compliant_dataframer   Ϊagg©r   ΪaggsΪ
named_aggss      r   r'   zGroupBy.agg   sU    πd 9488Χ8Ρ8Έ$ΠMΐ*ΡMΡjΨxxΧ1Ρ1ΨDMMΧΡtΠ2 zΡ2σ
π 	
r"   c              #  σh    K    fd j                   j                  «       D «       E d {    y 7 ­w)Nc              3  σp   K   | ]-  \  }}t        |«      j                  j                  |«      f / y ­w)N)r   r   r&   )Ϊ.0Ϊkeyr   r   s      r   Ϊ	<genexpr>z#GroupBy.__iter__.<locals>.<genexpr>v   s5   ψθ ψ π 
α5	bτ c]DHHΧ>Ρ>ΈrΣBΤCΩ5ωs   36)r   Ϊ__iter__)r   s   `r   r0   zGroupBy.__iter__u   s+   ψθ ψ σ
ΰ!]]Χ3Ρ3Τ5σ
χ 	
ς 	
ϊs   '2ͺ0«2N)r   r   r   Ϊstrr   ΪboolΪreturnΪNone)r)   ϊIntoExpr | Iterable[IntoExpr]r*   r   r3   r   )r3   z Iterator[tuple[Any, DataFrameT]])Ϊ__name__Ϊ
__module__Ϊ__qualname__r    r'   r0   © r"   r   r   r      s.    σ
πU
Ψ2πU
ΨBJπU
ΰ	σU
τn
r"   r   c                  σ(    e Zd ZddZ	 	 	 	 	 	 ddZy)ΪLazyGroupByc               σ΄    t        t        t           |«      | _        || _         | j                  j
                  j                  | j                  d|i| _        y r   )r
   r   r   r   r   r   r   r   r   s       r   r    zLazyGroupBy.__init__}   r!   r"   c                σͺ     | j                   j                  |i |€\  }}| j                   j                   | j                  j                  |i |€«      S )uθ	  Compute aggregations for each group of a group by operation.

        If a library does not support lazy execution, then this is a no-op.

        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_pl = pl.LazyFrame(
            ...     {
            ...         "a": ["a", "b", "a", "b", "c"],
            ...         "b": [1, 2, 1, 3, 3],
            ...         "c": [5, 4, 3, 2, 1],
            ...     }
            ... )

            We define library agnostic functions:

            >>> def agnostic_func_one_col(lf_native: IntoFrameT) -> IntoFrameT:
            ...     lf = nw.from_native(lf_native)
            ...     return nw.to_native(lf.group_by("a").agg(nw.col("b").sum()).sort("a"))

            >>> def agnostic_func_mult_col(lf_native: IntoFrameT) -> IntoFrameT:
            ...     lf = nw.from_native(lf_native)
            ...     return nw.to_native(lf.group_by("a", "b").agg(nw.sum("c")).sort("a", "b"))

            We can then pass a lazy frame and materialise it with `collect`:

            >>> agnostic_func_one_col(lf_pl).collect()
            shape: (3, 2)
            βββββββ¬ββββββ
            β a   β b   β
            β --- β --- β
            β str β i64 β
            βββββββͺββββββ‘
            β a   β 2   β
            β b   β 5   β
            β c   β 3   β
            βββββββ΄ββββββ
            >>> agnostic_func_mult_col(lf_pl).collect()
            shape: (4, 3)
            βββββββ¬ββββββ¬ββββββ
            β a   β b   β c   β
            β --- β --- β --- β
            β str β i64 β i64 β
            βββββββͺββββββͺββββββ‘
            β a   β 1   β 8   β
            β b   β 2   β 4   β
            β b   β 3   β 2   β
            β c   β 3   β 1   β
            βββββββ΄ββββββ΄ββββββ
        r$   r(   s      r   r'   zLazyGroupBy.agg   sU    πD 9488Χ8Ρ8Έ$ΠMΐ*ΡMΡjΨxxΧ1Ρ1ΨDMMΧΡtΠ2 zΡ2σ
π 	
r"   N)r   r   r   r1   r   r2   r3   r4   )r)   r5   r*   r   r3   r   )r6   r7   r8   r    r'   r9   r"   r   r;   r;   |   s(    σ
πE
Ψ2πE
ΨBJπE
ΰ	τE
r"   r;   N)Ϊ
__future__r   Ϊtypingr   r   r   r   r   r	   r
   Ϊnarwhals.dataframer   r   Ϊnarwhals.utilsr   Ϊnarwhals.typingr   r   r   r   r;   r9   r"   r   Ϊ<module>rC      sg   πέ "ε  έ έ έ έ έ έ ε (έ (έ #αέ(α\Σ"
Ω\Σ"
τc
gjΡ!τ c
τLM
'*Ρ%υ M
r"   