From dbe740753b7de6cf78ce7f613f9a6b4f51f4c3df Mon Sep 17 00:00:00 2001 From: Frankie Robertson Date: Fri, 5 Dec 2025 12:49:25 +0200 Subject: [PATCH] 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. --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/frame.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f6073e3b17e83..3dd8b8561ed8c 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -239,6 +239,7 @@ Other enhancements - Added a new :meth:`DataFrame.from_arrow` method to import any Arrow-compatible tabular data object into a pandas :class:`DataFrame` through the `Arrow PyCapsule Protocol `__ (:issue:`59631`) +- :meth:`DataFrame.itertuples` is now annotated to return an iterable of Any to accommodate dynamic namedtuple types .. --------------------------------------------------------------------------- .. _whatsnew_300.notable_bug_fixes: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 79f1c15d26ee6..9af142455a08e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1598,9 +1598,17 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]: s._mgr.add_references(self._mgr) yield k, s + @overload + def itertuples(self, index: bool, name: None) -> Iterable[tuple[Any, ...]]: + ... + + @overload + def itertuples(self, index: bool, name: str) -> Iterable[Any]: + ... + def itertuples( self, index: bool = True, name: str | None = "Pandas" - ) -> Iterable[tuple[Any, ...]]: + ) -> Iterable[tuple[Any, ...]] | Iterable[Any]: """ Iterate over DataFrame rows as namedtuples.