-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add ignore_index for df.drop_duplicates #30405
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 12 commits
7e461a1
1314059
8bcb313
3faf5cc
bdda8ca
6e76e56
c12beb6
1b6dc51
17dbcb0
a173eea
4a37e8f
79a49e1
5fb6e54
a8552d8
6eaff2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4587,6 +4587,7 @@ def drop_duplicates( | |
subset: Optional[Union[Hashable, Sequence[Hashable]]] = None, | ||
keep: Union[str, bool] = "first", | ||
inplace: bool = False, | ||
ignore_index: bool = False, | ||
) -> Optional["DataFrame"]: | ||
""" | ||
Return DataFrame with duplicate rows removed. | ||
|
@@ -4606,6 +4607,10 @@ def drop_duplicates( | |
- False : Drop all duplicates. | ||
inplace : bool, default False | ||
Whether to drop duplicates in place or to return a copy. | ||
ignore_index : bool, default False | ||
If True, the resulting axis will be labeled 0, 1, …, n - 1. | ||
|
||
.. versionadded:: 1.0.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -4621,9 +4626,16 @@ def drop_duplicates( | |
if inplace: | ||
(inds,) = (-duplicated)._ndarray_values.nonzero() | ||
new_data = self._data.take(inds) | ||
|
||
if ignore_index: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new_data.axes[1] = ibase.default_index(len(inds)) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._update_inplace(new_data) | ||
else: | ||
return self[-duplicated] | ||
result = self[-duplicated] | ||
|
||
if ignore_index: | ||
result.index = ibase.default_index(sum(-duplicated)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
return result | ||
|
||
return None | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -477,3 +477,46 @@ def test_drop_duplicates_inplace(): | |
expected = orig2.drop_duplicates(["A", "B"], keep=False) | ||
result = df2 | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"origin_dict, output_dict, ignore_index, output_index", | ||
[ | ||
({"A": [2, 2, 3]}, {"A": [2, 3]}, True, [0, 1]), | ||
({"A": [2, 2, 3]}, {"A": [2, 3]}, False, [0, 2]), | ||
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, True, [0, 1]), | ||
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, False, [0, 2]), | ||
], | ||
) | ||
def test_drop_duplicates_ignore_index_inplace_false( | ||
origin_dict, output_dict, ignore_index, output_index | ||
): | ||
# GH 30114 | ||
df = DataFrame(origin_dict) | ||
result = df.drop_duplicates(ignore_index=ignore_index) | ||
|
||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected = DataFrame(output_dict, index=output_index) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# to verify if original dataframe is not mutated | ||
tm.assert_frame_equal(df, DataFrame(origin_dict)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you incorporate these in the above test as well. can you also assert if inplace== True that the input is unchanged (copy it before and check equality) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. incorporated, and added test thanks @jreback |
||
"origin_dict, output_dict, ignore_index, output_index", | ||
[ | ||
({"A": [2, 2, 3]}, {"A": [2, 3]}, True, [0, 1]), | ||
({"A": [2, 2, 3]}, {"A": [2, 3]}, False, [0, 2]), | ||
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, True, [0, 1]), | ||
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, False, [0, 2]), | ||
], | ||
) | ||
def test_drop_duplicates_ignore_index_inplace_true( | ||
origin_dict, output_dict, ignore_index, output_index | ||
): | ||
# GH 30114, to check if correct when inplace is True | ||
df = DataFrame(origin_dict) | ||
df.drop_duplicates(ignore_index=ignore_index, inplace=True) | ||
|
||
expected = DataFrame(output_dict, index=output_index) | ||
tm.assert_frame_equal(df, expected) |
Uh oh!
There was an error while loading. Please reload this page.