-
-
Notifications
You must be signed in to change notification settings - Fork 154
itertuples #842
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
itertuples #842
Conversation
pandas-stubs/core/frame.pyi
Outdated
| def itertuples( | ||
| self, index: _bool = ..., name: _str | None = ... | ||
| ) -> Iterable[tuple[Any, ...]]: ... | ||
| ) -> Iterable[Any]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we could do something like this:
class _PandasNamedTuple(tuple[Any,...]):
def __getattr__(self, field: str) -> Any: ...Then have itertuples() return Iterable[_PandasNamedTuple]
Then at least you know you're getting a tuple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice solution!
tests/test_frame.py
Outdated
| Any, | ||
| Callable, | ||
| Generic, | ||
| TypeAlias, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to come from typing_extensions. Failed under python 3.9
Dr-Irv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you look at test_types_itertuples() in test_frame.py and see if that should be modified? Maybe introduce check(assert_type in there just to be sure.
pandas-stubs/core/frame.pyi
Outdated
| def __rtruediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ... | ||
|
|
||
| class _PandasNamedTuple(tuple[Any, ...]): | ||
| def __getattr__(self, field: str) -> Any: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking we should return Scalar here, because we also return Scalar when someone does df.loc[3, "a"]
While it's true that some non-scalar value could be an individual element of a DataFrame, I've taken the philosophy of limiting the types to what is "normal" usage, and if you put a funky type in a DataFrame or Series, then you can do a cast to fix it. I've done that in some of our application code when we have lists or other objects inside a Series or DataFrame.
Dr-Irv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @twoertwein
assert_type()to assert the type of any return value