Skip to content

Commit b7e6230

Browse files
seljaksmliu08
authored andcommitted
ENH: Add copy-on-write to DataFrame.drop (pandas-dev#49689)
1 parent 3756e4f commit b7e6230

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

pandas/core/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -4483,6 +4483,7 @@ def _drop_axis(
44834483
indexer,
44844484
axis=bm_axis,
44854485
allow_dups=True,
4486+
copy=None,
44864487
only_slice=only_slice,
44874488
)
44884489
result = self._constructor(new_mgr)

pandas/core/internals/array_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def reindex_indexer(
546546
axis: AxisInt,
547547
fill_value=None,
548548
allow_dups: bool = False,
549-
copy: bool = True,
549+
copy: bool | None = True,
550550
# ignored keywords
551551
only_slice: bool = False,
552552
# ArrayManager specific keywords
@@ -570,7 +570,7 @@ def _reindex_indexer(
570570
axis: AxisInt,
571571
fill_value=None,
572572
allow_dups: bool = False,
573-
copy: bool = True,
573+
copy: bool | None = True,
574574
use_na_proxy: bool = False,
575575
) -> T:
576576
"""

pandas/tests/copy_view/test_methods.py

+19
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ def test_reindex_columns(using_copy_on_write):
132132
tm.assert_frame_equal(df, df_orig)
133133

134134

135+
def test_drop_on_column(using_copy_on_write):
136+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
137+
df_orig = df.copy()
138+
df2 = df.drop(columns="a")
139+
df2._mgr._verify_integrity()
140+
141+
if using_copy_on_write:
142+
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
143+
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
144+
else:
145+
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
146+
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
147+
df2.iloc[0, 0] = 0
148+
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
149+
if using_copy_on_write:
150+
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
151+
tm.assert_frame_equal(df, df_orig)
152+
153+
135154
def test_select_dtypes(using_copy_on_write):
136155
# Case: selecting columns using `select_dtypes()` returns a new dataframe
137156
# + afterwards modifying the result

0 commit comments

Comments
 (0)