-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: Add typing for pandas.core.frame.dropna #38968
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
Changes from 1 commit
5d24561 f2733e3 f2e2bcf 63d0410 bc0c5d2 55ef7f9 5480288 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
As the `Optional[DataFrame]` return type creates a union of incompatible types (DataFrame and None), we overload the signature on the value of the `inplace` parameter, to force the return of a DataFrame when inplace is false. Fixes GH38948
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| | @@ -5069,6 +5069,28 @@ def notna(self) -> DataFrame: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def notnull(self) -> DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ~self.isna() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Overload function signature to prevent union of conflicting types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # As Optional[DataFrame] is really Union[DataFrame, None] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def dropna( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| axis: Axis = ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| how: str = ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| thresh: Optional[int] = ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inplace: Literal[False] = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | |
| # https://github.com/python/mypy/issues/6580 | |
| # Overloaded function signatures 1 and 2 overlap with incompatible return types | |
| def reset_index( # type: ignore[misc] | |
| self, | |
| level: Optional[Union[Hashable, Sequence[Hashable]]] = ..., | |
| drop: bool = ..., | |
| inplace: Literal[False] = ..., | |
| col_level: Hashable = ..., | |
| col_fill: Label = ..., | |
| ) -> DataFrame: | |
| ... | |
| @overload | |
| def reset_index( | |
| self, | |
| level: Optional[Union[Hashable, Sequence[Hashable]]] = ..., | |
| drop: bool = ..., | |
| inplace: Literal[True] = ..., | |
| col_level: Hashable = ..., | |
| col_fill: Label = ..., | |
| ) -> None: | |
| ... | |
| def reset_index( | |
| self, | |
| level: Optional[Union[Hashable, Sequence[Hashable]]] = None, | |
| drop: bool = False, | |
| inplace: bool = False, | |
| col_level: Hashable = 0, | |
| col_fill: Label = "", | |
| ) -> Optional[DataFrame]: |
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.
yeah in this example the defaults are not specified in the overloaded signatures so I think you should get rid of those
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.
That example also explains the mypy error (it's caused to a mypy bug). So to get mypy passing you need to add a type ignore (and a comment with the link to mypy issue and the error message above the overloaded signature as done in the reset_index example)
Those things being done I think this will be good to go in
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.
Gotcha! Changes forthcoming...
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.
we now have an IndexLabel alias in pandas._typing that could be used for subset.
| subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., | |
| subset: Optional[IndexLabel] = ..., |
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'd drop this comment