Skip to content

Commit

Permalink
Add ignore_index keyword arg in dropna and drop_duplicates (Par…
Browse files Browse the repository at this point in the history
…t of GH624) (#1030)

* add ignore_index keyword parameter to Series and DF dropna and drop_duplicates

* use assert_type instead

* reverse overloads order, remove ellipsis when inplace=True
  • Loading branch information
hyperc54 authored Nov 13, 2024
1 parent 0ab562c commit df1cd38
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
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

0 comments on commit df1cd38

Please sign in to comment.