L iddlmZddlmZmZmZmZddlmZddl m Z ddl m Z ddl mZerddlmZmZmZddlmZdd lmZdd lmZed d ZGddeeZGddeeZy)) annotations) TYPE_CHECKINGAnyGenericTypeVaris_scalar_like)tupleify)InvalidOperationError) DataFrameT)IterableIteratorSequence)CompliantExprAny) LazyFrame)Expr LazyFrameTzLazyFrame[Any])boundc4eZdZ ddZddZddZy)GroupByc||_||_|jjj|j||_yN)drop_null_keys_df_keys_compliant_framegroup_by_groupedselfdfkeysrs W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/narwhals/group_by.py__init__zGroupBy.__init__< " 11:: JJ~;  c|jj|i|}td|Ds d}t||jj |j j |S)uCompute 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 c32K|]}t|ywNr.0xs r$ zGroupBy.agg..L=>!$=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_extractallr _with_compliantraggr!aggs named_aggscompliant_aggsmsgs r$r6z GroupBy.agg#smP766K K=n==B  (, ,xx''(9 (9(9>(JKKr'c#hKfdjjDEd{y7w)Nc3pK|]-\}}t|jj|f/ywr*)r rr5)r,keyr"r!s r$r.z#GroupBy.__iter__..Ws5 bc]DHH44R8 9 s36)r__iter__)r!s`r$r?zGroupBy.__iter__Vs+ !]]335   s '202N)r"r r#*Sequence[str] | Sequence[CompliantExprAny]rboolreturnNone)r8Expr | Iterable[Expr]r9rrBr )rBz Iterator[tuple[Any, DataFrameT]])__name__ __module__ __qualname__r%r6r?r'r$rrs9    9       1Lf r'rc,eZdZ ddZddZy) LazyGroupByc||_||_|jjj|j||_yrrr s r$r%zLazyGroupBy.__init__^r&r'c|jj|i|}td|Ds d}t||jj |j j |S)uCompute 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 │| |└─────┴─────┴─────┘| └───────────────────┘ c32K|]}t|ywr*rr+s r$r.z"LazyGroupBy.agg..r/r0r1r2r7s r$r6zLazyGroupBy.agglsmj766K K=n==B  (, ,xx''(9 (9(9>(JKKr'N)r"rr#r@rrArBrC)r8rDr9rrBr)rErFrGr%r6rHr'r$rJrJ]s3    9       >Lr'rJN) __future__rtypingrrrrnarwhals._expression_parsingr narwhals._utilsr narwhals.exceptionsr narwhals.typingr collections.abcr rrnarwhals._compliant.typingrnarwhals.dataframer narwhals.exprrrrrJrHr'r$rXsb"777$5&<<;," \)9 : F gj!F RML'*%MLr'