Skip to content

ENH: Add lazy copy for drop duplicates #50431

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 2 commits into from
Dec 28, 2022
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
6 changes: 5 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3718,6 +3718,10 @@ def _getitem_bool_array(self, key):
# check_bool_indexer will throw exception if Series key cannot
# be reindexed to match DataFrame rows
key = check_bool_indexer(self.index, key)

if key.all():
return self.copy(deep=None)

indexer = key.nonzero()[0]
return self._take_with_is_copy(indexer, axis=0)

Expand Down Expand Up @@ -6418,7 +6422,7 @@ def drop_duplicates(
4 Indomie pack 5.0
"""
if self.empty:
return self.copy()
return self.copy(deep=None)

inplace = validate_bool_kwarg(inplace, "inplace")
ignore_index = validate_bool_kwarg(ignore_index, "ignore_index")
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,11 @@ def test_head_tail(method, using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_assign(using_copy_on_write):
@pytest.mark.parametrize("method", ["assign", "drop_duplicates"])
def test_assign_drop_duplicates(using_copy_on_write, method):
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
df2 = df.assign()
df2 = getattr(df, method)()
df2._mgr._verify_integrity()

if using_copy_on_write:
Expand Down