Skip to content

Commit c4095bc

Browse files
committed
refactor: Make PandasLikeDataFrame compliant
1 parent 927cf69 commit c4095bc

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

narwhals/_pandas_like/dataframe.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any
55
from typing import Iterator
66
from typing import Literal
7+
from typing import Mapping
78
from typing import Sequence
89
from typing import cast
910
from typing import overload
@@ -356,17 +357,15 @@ def iter_rows(
356357
*,
357358
named: bool,
358359
buffer_size: int,
359-
) -> Iterator[list[tuple[Any, ...]]] | Iterator[list[dict[str, Any]]]:
360+
) -> Iterator[tuple[Any, ...]] | Iterator[dict[str, Any]]:
360361
# The param ``buffer_size`` is only here for compatibility with the Polars API
361362
# and has no effect on the output.
362363
if not named:
363364
yield from self._native_frame.itertuples(index=False, name=None)
364365
else:
365366
col_names = self._native_frame.columns
366-
yield from (
367-
dict(zip(col_names, row))
368-
for row in self._native_frame.itertuples(index=False)
369-
) # type: ignore[misc]
367+
for row in self._native_frame.itertuples(index=False):
368+
yield dict(zip(col_names, row))
370369

371370
@property
372371
def schema(self: Self) -> dict[str, DType]:
@@ -418,7 +417,7 @@ def select(self: PandasLikeDataFrame, *exprs: PandasLikeExpr) -> PandasLikeDataF
418417
return self._from_native_frame(df, validate_column_names=True)
419418

420419
def drop_nulls(
421-
self: PandasLikeDataFrame, subset: list[str] | None
420+
self: PandasLikeDataFrame, subset: Sequence[str] | None
422421
) -> PandasLikeDataFrame:
423422
if subset is None:
424423
return self._from_native_frame(
@@ -445,8 +444,8 @@ def with_row_index(self: Self, name: str) -> Self:
445444
)
446445
)
447446

448-
def row(self: Self, row: int) -> tuple[Any, ...]:
449-
return tuple(x for x in self._native_frame.iloc[row])
447+
def row(self: Self, index: int) -> tuple[Any, ...]:
448+
return tuple(x for x in self._native_frame.iloc[index])
450449

451450
def filter(
452451
self: PandasLikeDataFrame, predicate: PandasLikeExpr | list[bool]
@@ -494,7 +493,7 @@ def with_columns(
494493
)
495494
return self._from_native_frame(df, validate_column_names=False)
496495

497-
def rename(self: Self, mapping: dict[str, str]) -> Self:
496+
def rename(self: Self, mapping: Mapping[str, str]) -> Self:
498497
return self._from_native_frame(
499498
rename(
500499
self._native_frame,
@@ -504,7 +503,7 @@ def rename(self: Self, mapping: dict[str, str]) -> Self:
504503
)
505504
)
506505

507-
def drop(self: Self, columns: list[str], strict: bool) -> Self: # noqa: FBT001
506+
def drop(self: Self, columns: Sequence[str], strict: bool) -> Self: # noqa: FBT001
508507
to_drop = parse_columns_to_drop(
509508
compliant_frame=self, columns=columns, strict=strict
510509
)
@@ -597,8 +596,8 @@ def join(
597596
other: Self,
598597
*,
599598
how: Literal["left", "inner", "cross", "anti", "semi"],
600-
left_on: list[str] | None,
601-
right_on: list[str] | None,
599+
left_on: Sequence[str] | None,
600+
right_on: Sequence[str] | None,
602601
suffix: str,
603602
) -> Self:
604603
if how == "cross":
@@ -655,7 +654,7 @@ def join(
655654
other_native = rename(
656655
select_columns_by_name(
657656
other._native_frame,
658-
right_on,
657+
list(right_on),
659658
self._backend_version,
660659
self._implementation,
661660
),
@@ -684,7 +683,7 @@ def join(
684683
rename(
685684
select_columns_by_name(
686685
other._native_frame,
687-
right_on,
686+
list(right_on),
688687
self._backend_version,
689688
self._implementation,
690689
),
@@ -735,8 +734,8 @@ def join_asof(
735734
*,
736735
left_on: str | None,
737736
right_on: str | None,
738-
by_left: list[str] | None,
739-
by_right: list[str] | None,
737+
by_left: Sequence[str] | None,
738+
by_right: Sequence[str] | None,
740739
strategy: Literal["backward", "forward", "nearest"],
741740
suffix: str,
742741
) -> Self:
@@ -768,7 +767,7 @@ def tail(self: Self, n: int) -> Self:
768767

769768
def unique(
770769
self: Self,
771-
subset: list[str] | None,
770+
subset: Sequence[str] | None,
772771
*,
773772
keep: Literal["any", "first", "last", "none"],
774773
maintain_order: bool | None = None,
@@ -1068,8 +1067,8 @@ def sample(
10681067

10691068
def unpivot(
10701069
self: Self,
1071-
on: list[str] | None,
1072-
index: list[str] | None,
1070+
on: Sequence[str] | None,
1071+
index: Sequence[str] | None,
10731072
variable_name: str,
10741073
value_name: str,
10751074
) -> Self:

narwhals/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,13 +1399,13 @@ def generate_repr(header: str, native_repr: str) -> str:
13991399
)
14001400

14011401

1402-
def check_column_exists(columns: list[str], subset: list[str] | None) -> None:
1402+
def check_column_exists(columns: Sequence[str], subset: Sequence[str] | None) -> None:
14031403
if subset is not None and (missing := set(subset).difference(columns)):
14041404
msg = f"Column(s) {sorted(missing)} not found in {columns}"
14051405
raise ColumnNotFoundError(msg)
14061406

14071407

1408-
def check_column_names_are_unique(columns: list[str]) -> None:
1408+
def check_column_names_are_unique(columns: Sequence[str]) -> None:
14091409
len_unique_columns = len(set(columns))
14101410
if len(columns) != len_unique_columns:
14111411
from collections import Counter

0 commit comments

Comments
 (0)