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.