Skip to content
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

Add ignore_index keyword arg in dropna and drop_duplicates (Part of GH624) #1030

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 23 additions & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ class DataFrame(NDFrame, OpsMixin):
thresh: int | None = ...,
subset: ListLikeU | Scalar | None = ...,
inplace: Literal[True],
ignore_index: _bool = ...,
) -> None: ...
@overload
def dropna(
Expand All @@ -890,6 +891,7 @@ class DataFrame(NDFrame, OpsMixin):
thresh: int | None = ...,
subset: ListLikeU | Scalar | None = ...,
inplace: Literal[False] = ...,
ignore_index: _bool = ...,
) -> DataFrame: ...
@overload
def dropna(
Expand All @@ -900,15 +902,35 @@ class DataFrame(NDFrame, OpsMixin):
thresh: int | None = ...,
subset: ListLikeU | Scalar | None = ...,
inplace: _bool | None = ...,
ignore_index: _bool = ...,
) -> DataFrame | None: ...
@overload
def drop_duplicates(
self,
subset: Hashable | Iterable[Hashable] | None = ...,
*,
keep: NaPosition | _bool = ...,
inplace: _bool = ...,
inplace: Literal[True],
ignore_index: _bool = ...,
) -> None: ...
@overload
def drop_duplicates(
self,
subset: Hashable | Iterable[Hashable] | None = ...,
*,
keep: NaPosition | _bool = ...,
inplace: Literal[False] = ...,
ignore_index: _bool = ...,
) -> DataFrame: ...
@overload
def drop_duplicates(
self,
subset: Hashable | Iterable[Hashable] | None = ...,
*,
keep: NaPosition | _bool = ...,
inplace: _bool = ...,
ignore_index: _bool = ...,
) -> DataFrame | None: ...
def duplicated(
self,
subset: Hashable | Iterable[Hashable] | None = ...,
Expand Down
25 changes: 20 additions & 5 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -726,15 +726,27 @@ class Series(IndexOpsMixin[S1], NDFrame):
def unique(self) -> np.ndarray: ...
@overload
def drop_duplicates(
self, *, keep: NaPosition | Literal[False] = ..., inplace: Literal[False] = ...
) -> Series[S1]: ...
self,
*,
keep: NaPosition | Literal[False] = ...,
inplace: Literal[True],
ignore_index: _bool = ...,
) -> None: ...
@overload
def drop_duplicates(
self, *, keep: NaPosition | Literal[False] = ..., inplace: Literal[True]
) -> None: ...
self,
*,
keep: NaPosition | Literal[False] = ...,
inplace: Literal[False] = ...,
ignore_index: _bool = ...,
) -> Series[S1]: ...
@overload
def drop_duplicates(
self, *, keep: NaPosition | Literal[False] = ..., inplace: bool = ...
self,
*,
keep: NaPosition | Literal[False] = ...,
inplace: bool = ...,
ignore_index: _bool = ...,
) -> Series[S1] | None: ...
def duplicated(self, keep: NaPosition | Literal[False] = ...) -> Series[_bool]: ...
def idxmax(
Expand Down Expand Up @@ -1148,6 +1160,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex = ...,
inplace: Literal[True],
how: Literal["any", "all"] | None = ...,
ignore_index: _bool = ...,
) -> None: ...
@overload
def dropna(
Expand All @@ -1156,6 +1169,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex = ...,
inplace: Literal[False] = ...,
how: Literal["any", "all"] | None = ...,
ignore_index: _bool = ...,
) -> Series[S1]: ...
@overload
def dropna(
Expand All @@ -1164,6 +1178,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex = ...,
inplace: _bool = ...,
how: Literal["any", "all"] | None = ...,
ignore_index: _bool = ...,
) -> Series[S1] | None: ...
def to_timestamp(
self,
Expand Down
26 changes: 23 additions & 3 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,22 @@ def test_arguments_drop() -> None:

def test_types_dropna() -> None:
df = pd.DataFrame(data={"col1": [np.nan, np.nan], "col2": [3, np.nan]})
res: pd.DataFrame = df.dropna()
res2: pd.DataFrame = df.dropna(axis=1, thresh=1)
res3: None = df.dropna(axis=0, how="all", subset=["col1"], inplace=True)
check(assert_type(df.dropna(), pd.DataFrame), pd.DataFrame)
check(assert_type(df.dropna(ignore_index=True), pd.DataFrame), pd.DataFrame)
check(assert_type(df.dropna(axis=1, thresh=1), pd.DataFrame), pd.DataFrame)
assert (
assert_type(df.dropna(axis=0, how="all", subset=["col1"], inplace=True), None)
is None
)
assert (
assert_type(
df.dropna(
axis=0, how="all", subset=["col1"], inplace=True, ignore_index=False
),
None,
)
is None
)


def test_types_drop_duplicates() -> None:
Expand All @@ -392,6 +405,13 @@ def test_types_drop_duplicates() -> None:
check(assert_type(df.drop_duplicates(["AAA"]), pd.DataFrame), pd.DataFrame)
check(assert_type(df.drop_duplicates(("AAA",)), pd.DataFrame), pd.DataFrame)
check(assert_type(df.drop_duplicates("AAA"), pd.DataFrame), pd.DataFrame)
assert assert_type(df.drop_duplicates("AAA", inplace=True), None) is None
check(
assert_type(
df.drop_duplicates("AAA", inplace=False, ignore_index=True), pd.DataFrame
),
pd.DataFrame,
)

if not PD_LTE_22:
check(assert_type(df.drop_duplicates({"AAA"}), pd.DataFrame), pd.DataFrame)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,20 @@ def test_types_drop_multilevel() -> None:
res: pd.Series = s.drop(labels="first", level=1)


def test_types_drop_duplicates() -> None:
s = pd.Series([1.0, 2.0, 2.0])
check(assert_type(s.drop_duplicates(), "pd.Series[float]"), pd.Series, float)
assert assert_type(s.drop_duplicates(inplace=True), None) is None
assert (
assert_type(s.drop_duplicates(inplace=True, ignore_index=False), None) is None
)


def test_types_dropna() -> None:
s = pd.Series([1.0, np.nan, np.nan])
check(assert_type(s.dropna(), "pd.Series[float]"), pd.Series, float)
assert assert_type(s.dropna(axis=0, inplace=True), None) is None
assert assert_type(s.dropna(axis=0, inplace=True, ignore_index=True), None) is None


def test_pop() -> None:
Expand Down
Loading