Skip to content

Commit 6d6cccc

Browse files
authored
BUG: interpolate not respecting CoW (#51147)
1 parent 0cedcbf commit 6d6cccc

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pandas/core/internals/managers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,15 @@ def diff(self: T, n: int, axis: AxisInt) -> T:
409409
axis = self._normalize_axis(axis)
410410
return self.apply("diff", n=n, axis=axis)
411411

412-
def interpolate(self: T, **kwargs) -> T:
413-
return self.apply("interpolate", **kwargs)
412+
def interpolate(self: T, inplace: bool, **kwargs) -> T:
413+
if inplace:
414+
# TODO(CoW) can be optimized to only copy those blocks that have refs
415+
if using_copy_on_write() and any(
416+
not self._has_no_reference_block(i) for i in range(len(self.blocks))
417+
):
418+
self = self.copy()
419+
420+
return self.apply("interpolate", inplace=inplace, **kwargs)
414421

415422
def shift(self: T, periods: int, axis: AxisInt, fill_value) -> T:
416423
axis = self._normalize_axis(axis)

pandas/tests/copy_view/test_methods.py

+16
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,22 @@ def test_asfreq_noop(using_copy_on_write):
11941194
tm.assert_frame_equal(df, df_orig)
11951195

11961196

1197+
def test_interpolate_creates_copy(using_copy_on_write):
1198+
# GH#51126
1199+
df = DataFrame({"a": [1.5, np.nan, 3]})
1200+
view = df[:]
1201+
expected = df.copy()
1202+
1203+
df.ffill(inplace=True)
1204+
df.iloc[0, 0] = 100.5
1205+
1206+
if using_copy_on_write:
1207+
tm.assert_frame_equal(view, expected)
1208+
else:
1209+
expected = DataFrame({"a": [100.5, 1.5, 3]})
1210+
tm.assert_frame_equal(view, expected)
1211+
1212+
11971213
def test_isetitem(using_copy_on_write):
11981214
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
11991215
df_orig = df.copy()

0 commit comments

Comments
 (0)