Skip to content

Commit 45bd637

Browse files
authored
ENH: Add lazy copy for truncate (#50477)
1 parent 2da7128 commit 45bd637

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
SingleArrayManager,
160160
)
161161
from pandas.core.internals.construction import mgr_to_mgr
162+
from pandas.core.internals.managers import _using_copy_on_write
162163
from pandas.core.missing import (
163164
clean_fill_method,
164165
clean_reindex_fill_method,
@@ -9950,7 +9951,7 @@ def truncate(
99509951
before=None,
99519952
after=None,
99529953
axis: Axis | None = None,
9953-
copy: bool_t = True,
9954+
copy: bool_t | None = None,
99549955
) -> NDFrameT:
99559956
"""
99569957
Truncate a Series or DataFrame before and after some index value.
@@ -10101,8 +10102,8 @@ def truncate(
1010110102
if isinstance(ax, MultiIndex):
1010210103
setattr(result, self._get_axis_name(axis), ax.truncate(before, after))
1010310104

10104-
if copy:
10105-
result = result.copy()
10105+
if copy or (copy is None and not _using_copy_on_write()):
10106+
result = result.copy(deep=copy)
1010610107

1010710108
return result
1010810109

pandas/tests/copy_view/test_methods.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,30 @@ def test_head_tail(method, using_copy_on_write):
371371
tm.assert_frame_equal(df, df_orig)
372372

373373

374+
@pytest.mark.parametrize(
375+
"kwargs",
376+
[
377+
{"before": "a", "after": "b", "axis": 1},
378+
{"before": 0, "after": 1, "axis": 0},
379+
],
380+
)
381+
def test_truncate(using_copy_on_write, kwargs):
382+
df = DataFrame({"a": [1, 2, 3], "b": 1, "c": 2})
383+
df_orig = df.copy()
384+
df2 = df.truncate(**kwargs)
385+
df2._mgr._verify_integrity()
386+
387+
if using_copy_on_write:
388+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
389+
else:
390+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
391+
392+
df2.iloc[0, 0] = 0
393+
if using_copy_on_write:
394+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
395+
tm.assert_frame_equal(df, df_orig)
396+
397+
374398
@pytest.mark.parametrize("method", ["assign", "drop_duplicates"])
375399
def test_assign_drop_duplicates(using_copy_on_write, method):
376400
df = DataFrame({"a": [1, 2, 3]})

pandas/tests/frame/methods/test_truncate.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ def test_truncate(self, datetime_frame, frame_or_series):
6666
before=ts.index[-1] - ts.index.freq, after=ts.index[0] + ts.index.freq
6767
)
6868

69-
def test_truncate_copy(self, datetime_frame):
70-
index = datetime_frame.index
71-
truncated = datetime_frame.truncate(index[5], index[10])
72-
truncated.values[:] = 5.0
73-
assert not (datetime_frame.values[5:11] == 5).any()
74-
7569
def test_truncate_nonsortedindex(self, frame_or_series):
7670
# GH#17935
7771

0 commit comments

Comments
 (0)