Skip to content

ENH: Add copy-on-write to DataFrame.drop #49689

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 4 commits into from
Nov 16, 2022
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
1 change: 1 addition & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4483,6 +4483,7 @@ def _drop_axis(
indexer,
axis=bm_axis,
allow_dups=True,
copy=None,
only_slice=only_slice,
)
result = self._constructor(new_mgr)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def reindex_indexer(
axis: AxisInt,
fill_value=None,
allow_dups: bool = False,
copy: bool = True,
copy: bool | None = True,
# ignored keywords
only_slice: bool = False,
# ArrayManager specific keywords
Expand All @@ -570,7 +570,7 @@ def _reindex_indexer(
axis: AxisInt,
fill_value=None,
allow_dups: bool = False,
copy: bool = True,
copy: bool | None = True,
use_na_proxy: bool = False,
) -> T:
"""
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def test_copy_shallow(using_copy_on_write):
# DataFrame methods returning new DataFrame using shallow copy


def test_drop_on_column(using_copy_on_write):
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=[10, 11, 12]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=[10, 11, 12]
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}

(I used a custom index for the reset_index test, but for the drop test, a custom index shouldn't matter)

)
df_orig = df.copy()
df2 = df.drop(columns="a")
df2._mgr._verify_integrity()

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
df2.iloc[0, 0] = 0
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)


def test_reset_index(using_copy_on_write):
# Case: resetting the index (i.e. adding a new column) + mutating the
# resulting dataframe
Expand Down