-
Notifications
You must be signed in to change notification settings - Fork 179
chore: Track ExpansionKind in ExprMetadata
#2266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8550da7
f7f0a90
2a799c8
40aea92
bac71ce
c8f483d
3fd7284
d06b4e5
291bd10
385fd13
2d4dd8a
a58c583
8e4bed2
274fd60
e333fa3
5e66ae5
94cc379
b5b1c92
8fa9f1c
3129332
8dbeb0e
ba235aa
3cdc967
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,8 +84,6 @@ class CompliantExpr(Protocol38[CompliantFrameT, CompliantSeriesOrNativeExprT_co] | |
| _version: Version | ||
| _evaluate_output_names: Callable[[CompliantFrameT], Sequence[str]] | ||
| _alias_output_names: Callable[[Sequence[str]], Sequence[str]] | None | ||
| _depth: int | ||
| _function_name: str | ||
| _metadata: ExprMetadata | None | ||
|
|
||
| def __call__( | ||
|
|
@@ -101,11 +99,12 @@ def from_column_names( | |
| evaluate_column_names: Callable[[CompliantFrameT], Sequence[str]], | ||
| /, | ||
| *, | ||
| function_name: str, | ||
| context: _FullContext, | ||
| ) -> Self: ... | ||
| @classmethod | ||
| def from_column_indices(cls, *column_indices: int, context: _FullContext) -> Self: ... | ||
| def from_column_indices( | ||
| cls: type[Self], *column_indices: int, context: _FullContext | ||
| ) -> Self: ... | ||
|
|
||
| def _with_metadata(self, metadata: ExprMetadata) -> Self: ... | ||
|
|
||
|
|
@@ -272,25 +271,64 @@ def __invert__(self) -> Self: ... | |
| def broadcast( | ||
| self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL] | ||
| ) -> Self: ... | ||
| def _is_multi_output_agg(self) -> bool: | ||
| """Return `True` for multi-output aggregations. | ||
| def _is_multi_output_unnamed(self) -> bool: | ||
| """Return `True` for multi-output aggregations without names. | ||
|
|
||
| For example, column `'a'` only appears in the output as a grouping key: | ||
|
|
||
| Here we skip the keys, else they would appear duplicated in the output: | ||
| df.group_by('a').agg(nw.all().sum()) | ||
|
|
||
| df.group_by("a").agg(nw.all().mean()) | ||
| It does not get included in: | ||
|
|
||
| nw.all().sum(). | ||
| """ | ||
| return self._function_name.split("->", maxsplit=1)[0] in {"all", "selector"} | ||
| assert self._metadata is not None # noqa: S101 | ||
| return self._metadata.expansion_kind.is_multi_unnamed() | ||
|
|
||
|
|
||
| class DepthTrackingExpr( | ||
| CompliantExpr[CompliantFrameT, CompliantSeriesOrNativeExprT_co], | ||
| Protocol38[CompliantFrameT, CompliantSeriesOrNativeExprT_co], | ||
| ): | ||
| _depth: int | ||
| _function_name: str | ||
|
|
||
| @classmethod | ||
| def from_column_names( | ||
| cls: type[Self], | ||
| evaluate_column_names: Callable[[CompliantFrameT], Sequence[str]], | ||
| /, | ||
| *, | ||
| context: _FullContext, | ||
| function_name: str = "", | ||
| ) -> Self: ... | ||
|
|
||
| def _is_elementary(self) -> bool: | ||
| """Check if expr is elementary. | ||
|
|
||
| Examples: | ||
| - nw.col('a').mean() # depth 1 | ||
| - nw.mean('a') # depth 1 | ||
| - nw.len() # depth 0 | ||
|
|
||
| as opposed to, say | ||
|
|
||
| - nw.col('a').filter(nw.col('b')>nw.col('c')).max() | ||
|
|
||
| Elementary expressions are the only ones supported properly in | ||
| pandas, PyArrow, and Dask. | ||
|
Comment on lines
+318
to
+319
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied directly from Maybe a description in the class doc could have some stuff from https://narwhals-dev.github.io/narwhals/how_it_works |
||
| """ | ||
| return self._depth < 2 | ||
|
|
||
| def __repr__(self) -> str: # pragma: no cover | ||
| return f"{type(self).__name__}(depth={self._depth}, function_name={self._function_name})" | ||
|
|
||
|
|
||
| class EagerExpr( | ||
| CompliantExpr[EagerDataFrameT, EagerSeriesT], | ||
| DepthTrackingExpr[EagerDataFrameT, EagerSeriesT], | ||
| Protocol38[EagerDataFrameT, EagerSeriesT], | ||
| ): | ||
| _call: Callable[[EagerDataFrameT], Sequence[EagerSeriesT]] | ||
| _depth: int | ||
| _function_name: str | ||
| _evaluate_output_names: Any | ||
| _alias_output_names: Any | ||
| _call_kwargs: dict[str, Any] | ||
|
|
||
| def __init__( | ||
|
|
@@ -310,9 +348,6 @@ def __init__( | |
| def __call__(self, df: EagerDataFrameT) -> Sequence[EagerSeriesT]: | ||
| return self._call(df) | ||
|
|
||
| def __repr__(self) -> str: # pragma: no cover | ||
| return f"{type(self).__name__}(depth={self._depth}, function_name={self._function_name})" | ||
|
|
||
| def __narwhals_namespace__( | ||
| self, | ||
| ) -> EagerNamespace[EagerDataFrameT, EagerSeriesT, Self]: ... | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ class CompliantThen(CompliantExpr[FrameT, SeriesT], Protocol38[FrameT, SeriesT, | |
| _call: Callable[[FrameT], Sequence[SeriesT]] | ||
| _when_value: CompliantWhen[FrameT, SeriesT, ExprT] | ||
| _function_name: str | ||
| _depth: int | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nuisance, can fix properly in a follow-up and align |
||
| _implementation: Implementation | ||
| _backend_version: tuple[int, ...] | ||
| _version: Version | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(#2266 (comment)) can be taken a step further by adding:
So this part wouldn't need to even know what an
ExpansionKindis, and having the line here be:But this works as well