Skip to content

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

Closed
wants to merge 7 commits into from
31 changes: 28 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5070,14 +5070,38 @@ def notna(self) -> DataFrame:
def notnull(self) -> DataFrame:
return ~self.isna()

@overload
# https://github.com/python/mypy/issues/6580
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure this comment has to go above the decorator?

Copy link
Author

Choose a reason for hiding this comment

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

It doesn't in pandas.core.frame.reset_index, which doesn't seem to cause any problems. Also, it passed the typing portion of CI/Checks build, so I think we are fine. Happy to move it if there is a style preference though!

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a preference since it passes CI

# Overloaded function signatures 1 and 2 overlap with incompatible return types
def dropna( # type: ignore[misc]
self,
axis: Axis = ...,
how: str = ...,
thresh: Optional[int] = ...,
subset: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
inplace: Literal[False] = ...,
) -> DataFrame:
...

@overload
def dropna(
self,
axis: Axis = ...,
how: str = ...,
thresh: Optional[int] = ...,
subset: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
Copy link
Member

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.

Suggested change
subset: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
subset: Optional[IndexLabel] = ...,

inplace: Literal[True] = ...,
) -> None:
...

def dropna(
self,
axis: Axis = 0,
how: str = "any",
thresh=None,
subset=None,
thresh: Optional[int] = None,
subset: Optional[Union[Hashable, Sequence[Hashable]]] = None,
inplace: bool = False,
):
) -> Optional[DataFrame]:
Copy link
Member

Choose a reason for hiding this comment

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

I see you used #37137 as a example. However the motivation for that PR was to remove an assert df is not None from the codebase and the return type of Optional[DataFrame] not touched.

Not needed to be done in this PR, but just FYI if the motivation for adding the types is for the public api, then the return type should be the same type as self for subclassed DataFrames.

"""
Remove missing values.

Expand Down Expand Up @@ -5216,6 +5240,7 @@ def dropna(

if inplace:
self._update_inplace(result)
return None
else:
return result

Expand Down