Skip to content
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

itertuples #842

Merged
merged 8 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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: 1 addition & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class DataFrame(NDFrame, OpsMixin):
def iterrows(self) -> Iterable[tuple[Hashable, Series]]: ...
def itertuples(
self, index: _bool = ..., name: _str | None = ...
) -> Iterable[tuple[Any, ...]]: ...
) -> Iterable[Any]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if we could do something like this:

class _PandasNamedTuple(tuple[Any,...]):
    def __getattr__(self, field: str) -> Any: ...

Then have itertuples() return Iterable[_PandasNamedTuple]

Then at least you know you're getting a tuple.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice solution!

def __len__(self) -> int: ...
@overload
def dot(self, other: DataFrame | ArrayLike) -> DataFrame: ...
Expand Down
9 changes: 9 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2949,3 +2949,12 @@ def test_frame_setitem_na() -> None:
df["x"] = df["y"] + pd.Timedelta(days=3)
df.loc[ind, :] = pd.NaT
df.iloc[[0, 2], :] = pd.NaT


def test_itertuples() -> None:
# GH 822
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})

for item in df.itertuples():
check(assert_type(item, Any), tuple)
assert_type(item.a, Any)