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 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
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
19 changes: 19 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ def test_reindex_columns(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


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]})
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"))
else:
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert not 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_select_dtypes(using_copy_on_write):
# Case: selecting columns using `select_dtypes()` returns a new dataframe
# + afterwards modifying the result
Expand Down