Skip to content

Commit dbe7407

Browse files
author
Frankie Robertson
committed
Type the return of itertuples as an iterable of Any
This is needed since it is a dynamically created NamedTuple. An overload is added for the case of a plain tuple return value when name is passed as None.
1 parent fa1360d commit dbe7407

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Other enhancements
239239
- Added a new :meth:`DataFrame.from_arrow` method to import any Arrow-compatible
240240
tabular data object into a pandas :class:`DataFrame` through the
241241
`Arrow PyCapsule Protocol <https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html>`__ (:issue:`59631`)
242+
- :meth:`DataFrame.itertuples` is now annotated to return an iterable of Any to accommodate dynamic namedtuple types
242243

243244
.. ---------------------------------------------------------------------------
244245
.. _whatsnew_300.notable_bug_fixes:

pandas/core/frame.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,9 +1598,17 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
15981598
s._mgr.add_references(self._mgr)
15991599
yield k, s
16001600

1601+
@overload
1602+
def itertuples(self, index: bool, name: None) -> Iterable[tuple[Any, ...]]:
1603+
...
1604+
1605+
@overload
1606+
def itertuples(self, index: bool, name: str) -> Iterable[Any]:
1607+
...
1608+
16011609
def itertuples(
16021610
self, index: bool = True, name: str | None = "Pandas"
1603-
) -> Iterable[tuple[Any, ...]]:
1611+
) -> Iterable[tuple[Any, ...]] | Iterable[Any]:
16041612
"""
16051613
Iterate over DataFrame rows as namedtuples.
16061614

0 commit comments

Comments
 (0)