Skip to content

TYP: misc typing fixes for pandas\core\frame.py #35990

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 2 commits into from
Aug 31, 2020
Merged
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
10 changes: 7 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ def iterrows(self) -> Iterable[Tuple[Label, Series]]:
s = klass(v, index=columns, name=k)
yield k, s

def itertuples(self, index=True, name="Pandas"):
def itertuples(self, index: bool = True, name: Optional[str] = "Pandas"):
"""
Iterate over DataFrame rows as namedtuples.

Expand Down Expand Up @@ -1088,7 +1088,11 @@ def itertuples(self, index=True, name="Pandas"):
arrays.extend(self.iloc[:, k] for k in range(len(self.columns)))

if name is not None:
itertuple = collections.namedtuple(name, fields, rename=True)
# https://github.com/python/mypy/issues/848
# error: namedtuple() expects a string literal as the first argument [misc]
Copy link
Member

Choose a reason for hiding this comment

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

cant do a cast or assert?

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, I've got the wrong mypy issue here.

cant do a cast or assert?

no, it has to be a string literal and doesn't look like mypy will change.

see python/mypy#9046 (comment)

we can remove this ignore with Final in Python 3.8 (and a later version of mypy)

itertuple = collections.namedtuple( # type: ignore
name, fields, rename=True
)
return map(itertuple._make, zip(*arrays))

# fallback to regular tuples
Expand Down Expand Up @@ -4591,7 +4595,7 @@ def set_index(
frame = self.copy()

arrays = []
names = []
names: List[Label] = []
if append:
names = list(self.index.names)
if isinstance(self.index, MultiIndex):
Expand Down