Skip to content

GH1217 Modify items/iterros/itertuples methods to return Iterator #1225

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

Merged
merged 5 commits into from
May 21, 2025

Conversation

loicdiridollou
Copy link
Member

Comment on lines 601 to 605
check(
assert_type(df.iterrows(), "Iterable[tuple[Hashable, pd.Series]]"),
assert_type(df.iterrows(), "Iterator[tuple[Hashable, pd.Series]]"),
Iterable,
tuple,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a test of this form for iterrows() and itertuples():

for t1, t2 in df.iterrows():
    check(assert_type(t1, Hashable), Hashable)
    check(assert_type(t2, pd.Series), pd.Series)

Comment on lines 631 to 633
for t1 in df.itertuples():
check(assert_type(t1, _PandasNamedTuple), _PandasNamedTuple)

Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't be using _PandasNamedTuple here in the call to check because what is happening internally is that pandas is creating a new class of type pandas.core.foo.Pandas, and if you use the name="Foobar" argument, the class is of the type pandas.core.foo.Foobar

But it's a dynamic class, so I think something like this would be appropriate:

for t1 in df.itertuples():
    assert_type(t1, _PandasNamedTuple)
    assert(t1.__class__.__name__ == "Pandas")
    assert(isinstance(t1.Index, int))
    assert(isinstance(t1.col1, int))  
    assert(isinstance(t1.col2, int))
    for k in [0, 1, 2]:
        assert(isinstance(t1[k], int))

And you could add a similar test for having name="Foobar" as the argument, and only the line that checks the name of the class would need to be changed.

assert isinstance(t1.Index, int)
assert isinstance(t1.col1, int)
assert isinstance(t1.col2, int)
for k in [0, 1]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This loop and the one below should be for k in [0,1,2] since the tuple has 3 components.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh yes correct I thought it was iterating over the number of elements.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

@Dr-Irv Dr-Irv merged commit 925584b into pandas-dev:main May 21, 2025
13 checks passed
@tilsche
Copy link

tilsche commented May 28, 2025

Thank you for the quick fix, merge and release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return type for DataFrame/Series iteration methods should be Iterator, not Iterable
3 participants