Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
aa6578e
fix(typing): Narrow `IntoDataFrame`
dangotbanned Apr 7, 2025
51c5b63
fix(typing): Remove `DataFrame` from `IntoFrame`
dangotbanned Apr 7, 2025
6cbbc74
fix(typing): Narrow `IntoLazyFrame`, `IntoFrame`
dangotbanned Apr 7, 2025
255ab27
fix(typing): Annotate `DataFrame._compliant_frame`
dangotbanned Apr 7, 2025
ba2f6e1
chore: Add missing `CompliantDataFrame.pivot`
dangotbanned Apr 7, 2025
1815752
fix(typing): Ensure `__iter__` is available on group_by
dangotbanned Apr 7, 2025
07deea2
chore(typing): Fix most of `DataFrame`
dangotbanned Apr 7, 2025
3881822
chore(typing): Ignore interchange `[type-var]`
dangotbanned Apr 7, 2025
375fabc
test(typing): Barely fix dodgy spark typing
dangotbanned Apr 7, 2025
21e80ef
fix: Implement `to_numpy` to catch args
dangotbanned Apr 7, 2025
c124985
fix(typing): Annotate `LazyFrame._compliant_frame`
dangotbanned Apr 7, 2025
831a6be
chore(typing): Ignore and add note for `spark_like` cast
dangotbanned Apr 7, 2025
1725f36
chore(typing): Partial `v1` backport
dangotbanned Apr 7, 2025
c4bed59
fix(typing): Just preserve `v1` behavior
dangotbanned Apr 7, 2025
6a9fd91
simplify
dangotbanned Apr 7, 2025
ed65ad2
try old `Union`
dangotbanned Apr 7, 2025
b97149d
Merge remote-tracking branch 'upstream/main' into narrow-type-var-frame
dangotbanned Apr 8, 2025
6a66779
docs(typing): Provide more context on what and why
dangotbanned Apr 8, 2025
675329c
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 8, 2025
5dd7825
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 9, 2025
a1c51ff
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 9, 2025
c2b328b
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 10, 2025
b66d1bf
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 12, 2025
ba525f1
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 12, 2025
c45f1f4
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 12, 2025
322b00d
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 12, 2025
d6cb16b
chore(typing): Use `Sequence[str]` in `pivot`
dangotbanned Apr 12, 2025
cbd60d9
refactor(typing): Use `PivotAgg`
dangotbanned Apr 12, 2025
53722e8
Merge remote-tracking branch 'upstream/main' into narrow-type-var-frame
dangotbanned Apr 12, 2025
e111fc3
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 13, 2025
23158d1
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 13, 2025
bc72941
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 14, 2025
a1fb349
Merge branch 'main' into narrow-type-var-frame
dangotbanned Apr 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions narwhals/_arrow/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,5 @@ def unpivot(
)
# TODO(Unassigned): Even with promote_options="permissive", pyarrow does not
# upcast numeric to non-numeric (e.g. string) datatypes

pivot = not_implemented()
14 changes: 13 additions & 1 deletion narwhals/_compliant/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
from typing_extensions import TypeAlias

from narwhals._compliant.group_by import CompliantGroupBy
from narwhals._compliant.group_by import DataFrameGroupBy
from narwhals._translate import IntoArrowTable
from narwhals.dtypes import DType
from narwhals.schema import Schema
from narwhals.typing import AsofJoinStrategy
from narwhals.typing import JoinStrategy
from narwhals.typing import LazyUniqueKeepStrategy
from narwhals.typing import PivotAgg
from narwhals.typing import SizeUnit
from narwhals.typing import UniqueKeepStrategy
from narwhals.typing import _2DArray
Expand Down Expand Up @@ -136,7 +138,7 @@ def gather_every(self, n: int, offset: int) -> Self: ...
def get_column(self, name: str) -> CompliantSeriesT: ...
def group_by(
self, *keys: str, drop_null_keys: bool
) -> CompliantGroupBy[Self, Any]: ...
) -> DataFrameGroupBy[Self, Any]: ...
def head(self, n: int) -> Self: ...
def item(self, row: int | None, column: int | str | None) -> Any: ...
def iter_columns(self) -> Iterator[CompliantSeriesT]: ...
Expand Down Expand Up @@ -165,6 +167,16 @@ def join_asof(
suffix: str,
) -> Self: ...
def lazy(self, *, backend: Implementation | None) -> CompliantLazyFrame[Any, Any]: ...
def pivot(
self,
on: Sequence[str],
*,
index: Sequence[str] | None,
values: Sequence[str] | None,
aggregate_function: PivotAgg | None,
sort_columns: bool,
separator: str,
) -> Self: ...
def rename(self, mapping: Mapping[str, str]) -> Self: ...
def row(self, index: int) -> tuple[Any, ...]: ...
def rows(
Expand Down
11 changes: 9 additions & 2 deletions narwhals/_compliant/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def __init__(
def agg(self, *exprs: CompliantExprT_contra) -> CompliantFrameT_co: ...


class DataFrameGroupBy(
CompliantGroupBy[CompliantDataFrameT_co, CompliantExprT_contra],
Protocol38[CompliantDataFrameT_co, CompliantExprT_contra],
):
def __iter__(self) -> Iterator[tuple[Any, CompliantDataFrameT_co]]: ...
Comment on lines +79 to +83
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FBruzzesi I forgot to mention this will probably be helpful in getting the typing working for (#2325).

It means you can use CompliantGroupBy for Polars* and put the unrelated parts here.

I needed to add it to resolve an unrelated issue (1815752)



class DepthTrackingGroupBy(
CompliantGroupBy[CompliantFrameT_co, DepthTrackingExprT_contra],
Protocol38[CompliantFrameT_co, DepthTrackingExprT_contra, NativeAggregationT_co],
Expand Down Expand Up @@ -132,9 +139,9 @@ def _leaf_name(cls, expr: DepthTrackingExprAny, /) -> NarwhalsAggregation | Any:

class EagerGroupBy(
DepthTrackingGroupBy[CompliantDataFrameT_co, EagerExprT_contra, str],
DataFrameGroupBy[CompliantDataFrameT_co, EagerExprT_contra],
Protocol38[CompliantDataFrameT_co, EagerExprT_contra],
):
def __iter__(self) -> Iterator[tuple[Any, CompliantDataFrameT_co]]: ...
): ...


class LazyGroupBy(
Expand Down
11 changes: 6 additions & 5 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from narwhals.typing import CompliantLazyFrame
from narwhals.typing import DTypeBackend
from narwhals.typing import JoinStrategy
from narwhals.typing import PivotAgg
from narwhals.typing import SizeUnit
from narwhals.typing import UniqueKeepStrategy
from narwhals.typing import _1DArray
Expand Down Expand Up @@ -1017,12 +1018,12 @@ def gather_every(self: Self, n: int, offset: int) -> Self:
return self._with_native(self.native.iloc[offset::n], validate_column_names=False)

def pivot(
self: Self,
on: list[str],
self,
on: Sequence[str],
*,
index: list[str] | None,
values: list[str] | None,
aggregate_function: Any | None,
index: Sequence[str] | None,
values: Sequence[str] | None,
aggregate_function: PivotAgg | None,
sort_columns: bool,
separator: str,
) -> Self:
Expand Down
7 changes: 4 additions & 3 deletions narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import suppress
from typing import TYPE_CHECKING
from typing import Any
from typing import Sequence
from typing import Sized
from typing import TypeVar
from typing import cast
Expand Down Expand Up @@ -652,9 +653,9 @@ def select_columns_by_name(

def pivot_table(
df: PandasLikeDataFrame,
values: list[str],
index: list[str],
columns: list[str],
values: Sequence[str],
index: Sequence[str],
columns: Sequence[str],
aggregate_function: str | None,
) -> Any:
dtypes = import_dtypes_module(df._version)
Expand Down
18 changes: 9 additions & 9 deletions narwhals/_polars/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from narwhals.typing import CompliantDataFrame
from narwhals.typing import CompliantLazyFrame
from narwhals.typing import JoinStrategy
from narwhals.typing import PivotAgg
from narwhals.typing import _2DArray
from narwhals.utils import Version
from narwhals.utils import _FullContext
Expand Down Expand Up @@ -77,7 +78,6 @@ class PolarsDataFrame:
select: Method[Self]
sort: Method[Self]
to_arrow: Method[pa.Table]
to_numpy: Method[_2DArray]
to_pandas: Method[pd.DataFrame]
unique: Method[Self]
with_columns: Method[Self]
Expand Down Expand Up @@ -232,6 +232,9 @@ def __array__(
return self.native.__array__(dtype)
return self.native.__array__(dtype)

def to_numpy(self, dtype: Any = None, *, copy: bool | None = None) -> _2DArray:
return self.native.to_numpy()

def collect_schema(self: Self) -> dict[str, DType]:
if self._backend_version < (1,):
return {
Expand Down Expand Up @@ -412,15 +415,12 @@ def unpivot(
)

def pivot(
self: Self,
on: list[str],
self,
on: Sequence[str],
*,
index: list[str] | None,
values: list[str] | None,
aggregate_function: Literal[
"min", "max", "first", "last", "sum", "mean", "median", "len"
]
| None,
index: Sequence[str] | None,
values: Sequence[str] | None,
aggregate_function: PivotAgg | None,
sort_columns: bool,
separator: str,
) -> Self:
Expand Down
Loading
Loading