Skip to content

Commit

Permalink
fix iter_rows (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdAbati authored Jul 29, 2024
1 parent f776324 commit 55c9a86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ def iter_rows(
if not named:
yield from self._native_dataframe.itertuples(index=False, name=None)
else:
col_names = self._native_dataframe.columns
yield from (
row._asdict() for row in self._native_dataframe.itertuples(index=False)
)
dict(zip(col_names, row))
for row in self._native_dataframe.itertuples(index=False)
) # type: ignore[misc]

@property
def schema(self) -> dict[str, DType]:
Expand Down
10 changes: 5 additions & 5 deletions tests/frame/rows_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
@pytest.mark.parametrize(
("named", "expected"),
[
(False, [(1, 4, 7.0), (3, 4, 8.0), (2, 6, 9.0)]),
(False, [(1, 4, 7.0, 5), (3, 4, 8.0, 6), (2, 6, 9.0, 7)]),
(
True,
[
{"a": 1, "b": 4, "z": 7.0},
{"a": 3, "b": 4, "z": 8.0},
{"a": 2, "b": 6, "z": 9.0},
{"a": 1, "_b": 4, "z": 7.0, "1": 5},
{"a": 3, "_b": 4, "z": 8.0, "1": 6},
{"a": 2, "_b": 6, "z": 9.0, "1": 7},
],
),
],
Expand All @@ -59,7 +59,7 @@ def test_iter_rows(
named: bool, # noqa: FBT001
expected: list[tuple[Any, ...]] | list[dict[str, Any]],
) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
data = {"a": [1, 3, 2], "_b": [4, 4, 6], "z": [7.0, 8, 9], "1": [5, 6, 7]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = list(df.iter_rows(named=named))
assert result == expected
Expand Down

0 comments on commit 55c9a86

Please sign in to comment.