Skip to content

ENH: updating subset types in drop_duplicates/duplicated #986

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

Merged
merged 8 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,15 @@ class DataFrame(NDFrame, OpsMixin):
) -> DataFrame | None: ...
def drop_duplicates(
self,
subset=...,
subset: Hashable | Iterable[Hashable] | None = ...,
*,
keep: NaPosition | _bool = ...,
inplace: _bool = ...,
ignore_index: _bool = ...,
) -> DataFrame: ...
def duplicated(
self,
subset: Hashable | Sequence[Hashable] | None = ...,
subset: Hashable | Iterable[Hashable] | None = ...,
keep: NaPosition | _bool = ...,
) -> Series: ...
@overload
Expand Down
29 changes: 28 additions & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import numpy as np
import numpy.typing as npt
import pandas as pd
from pandas._testing import ensure_clean
from pandas._testing import ensure_clean, assert_frame_equal
from pandas.core.resample import (
DatetimeIndexResampler,
Resampler,
Expand Down Expand Up @@ -368,6 +368,33 @@ def test_types_dropna() -> None:
res3: None = df.dropna(axis=0, how="all", subset=["col1"], inplace=True)


@pytest.mark.parametrize(
"drop_arg",
[
{"AAA"}, # set
["AAA"], # list
("AAA",), # tuple
{"AAA": None}, # dict
"AAA", # str
]
)
def test_types_drop_duplicates(drop_arg) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the purposes of testing type checks, you have to explicitly put in each of these arguments and can't parameterize. So you'd do:

check(assert_type(df.drop_duplicates({"AAA"}), pd.DataFrame), pd.DataFrame)
check(assert_type(df.drop_duplicates(["AAA"]), pd.DataFrame), pd.DataFrame)
# and similar for the other 3

Then you don't need to do assert_frame_equal()


# GH#59237
df = pd.DataFrame(
{
"AAA": ["foo", "bar", "foo", "bar", "foo", "bar", "bar", "foo"],
"B": ["one", "one", "two", "two", "two", "two", "one", "two"],
"C": [1, 1, 2, 2, 2, 2, 1, 2],
"D": range(8),
}
)
expected = df[:2]

result = df.drop_duplicates(drop_arg)
assert_frame_equal(result, expected)


def test_types_fillna() -> None:
df = pd.DataFrame(data={"col1": [np.nan, np.nan], "col2": [3, np.nan]})
res: pd.DataFrame = df.fillna(0)
Expand Down