Skip to content

Commit c27d97c

Browse files
committed
2 parents 491616b + a0071f9 commit c27d97c

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9908,7 +9908,7 @@ def shift(
99089908
2020-01-08 45 48 52
99099909
"""
99109910
if periods == 0:
9911-
return self.copy()
9911+
return self.copy(deep=None)
99129912

99139913
if freq is None:
99149914
# when freq is None, data is shifted, index is not

pandas/tests/copy_view/test_methods.py

+71
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,77 @@ def test_filter(using_copy_on_write, filter_kwargs):
244244
tm.assert_frame_equal(df, df_orig)
245245

246246

247+
def test_shift_no_op(using_copy_on_write):
248+
df = DataFrame(
249+
[[1, 2], [3, 4], [5, 6]],
250+
index=date_range("2020-01-01", "2020-01-03"),
251+
columns=["a", "b"],
252+
)
253+
df_orig = df.copy()
254+
df2 = df.shift(periods=0)
255+
256+
if using_copy_on_write:
257+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
258+
else:
259+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
260+
261+
df.iloc[0, 0] = 0
262+
if using_copy_on_write:
263+
assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b"))
264+
tm.assert_frame_equal(df2, df_orig)
265+
266+
267+
def test_shift_index(using_copy_on_write):
268+
df = DataFrame(
269+
[[1, 2], [3, 4], [5, 6]],
270+
index=date_range("2020-01-01", "2020-01-03"),
271+
columns=["a", "b"],
272+
)
273+
df2 = df.shift(periods=1, axis=0)
274+
275+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
276+
277+
278+
def test_shift_rows_freq(using_copy_on_write):
279+
df = DataFrame(
280+
[[1, 2], [3, 4], [5, 6]],
281+
index=date_range("2020-01-01", "2020-01-03"),
282+
columns=["a", "b"],
283+
)
284+
df_orig = df.copy()
285+
df_orig.index = date_range("2020-01-02", "2020-01-04")
286+
df2 = df.shift(periods=1, freq="1D")
287+
288+
if using_copy_on_write:
289+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
290+
else:
291+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
292+
293+
df.iloc[0, 0] = 0
294+
if using_copy_on_write:
295+
assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
296+
tm.assert_frame_equal(df2, df_orig)
297+
298+
299+
def test_shift_columns(using_copy_on_write):
300+
df = DataFrame(
301+
[[1, 2], [3, 4], [5, 6]], columns=date_range("2020-01-01", "2020-01-02")
302+
)
303+
df2 = df.shift(periods=1, axis=1)
304+
305+
assert np.shares_memory(get_array(df2, "2020-01-02"), get_array(df, "2020-01-01"))
306+
df.iloc[0, 1] = 0
307+
if using_copy_on_write:
308+
assert not np.shares_memory(
309+
get_array(df2, "2020-01-02"), get_array(df, "2020-01-01")
310+
)
311+
expected = DataFrame(
312+
[[np.nan, 1], [np.nan, 3], [np.nan, 5]],
313+
columns=date_range("2020-01-01", "2020-01-02"),
314+
)
315+
tm.assert_frame_equal(df2, expected)
316+
317+
247318
def test_pop(using_copy_on_write):
248319
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
249320
df_orig = df.copy()

0 commit comments

Comments
 (0)