|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas import DataFrame, Index, Series, date_range, offsets |
| 6 | +import pandas.util.testing as tm |
| 7 | + |
| 8 | + |
| 9 | +class TestDataFrameShift: |
| 10 | + def test_shift(self, datetime_frame, int_frame): |
| 11 | + # naive shift |
| 12 | + shiftedFrame = datetime_frame.shift(5) |
| 13 | + tm.assert_index_equal(shiftedFrame.index, datetime_frame.index) |
| 14 | + |
| 15 | + shiftedSeries = datetime_frame["A"].shift(5) |
| 16 | + tm.assert_series_equal(shiftedFrame["A"], shiftedSeries) |
| 17 | + |
| 18 | + shiftedFrame = datetime_frame.shift(-5) |
| 19 | + tm.assert_index_equal(shiftedFrame.index, datetime_frame.index) |
| 20 | + |
| 21 | + shiftedSeries = datetime_frame["A"].shift(-5) |
| 22 | + tm.assert_series_equal(shiftedFrame["A"], shiftedSeries) |
| 23 | + |
| 24 | + # shift by 0 |
| 25 | + unshifted = datetime_frame.shift(0) |
| 26 | + tm.assert_frame_equal(unshifted, datetime_frame) |
| 27 | + |
| 28 | + # shift by DateOffset |
| 29 | + shiftedFrame = datetime_frame.shift(5, freq=offsets.BDay()) |
| 30 | + assert len(shiftedFrame) == len(datetime_frame) |
| 31 | + |
| 32 | + shiftedFrame2 = datetime_frame.shift(5, freq="B") |
| 33 | + tm.assert_frame_equal(shiftedFrame, shiftedFrame2) |
| 34 | + |
| 35 | + d = datetime_frame.index[0] |
| 36 | + shifted_d = d + offsets.BDay(5) |
| 37 | + tm.assert_series_equal( |
| 38 | + datetime_frame.xs(d), shiftedFrame.xs(shifted_d), check_names=False |
| 39 | + ) |
| 40 | + |
| 41 | + # shift int frame |
| 42 | + int_shifted = int_frame.shift(1) # noqa |
| 43 | + |
| 44 | + # Shifting with PeriodIndex |
| 45 | + ps = tm.makePeriodFrame() |
| 46 | + shifted = ps.shift(1) |
| 47 | + unshifted = shifted.shift(-1) |
| 48 | + tm.assert_index_equal(shifted.index, ps.index) |
| 49 | + tm.assert_index_equal(unshifted.index, ps.index) |
| 50 | + tm.assert_numpy_array_equal( |
| 51 | + unshifted.iloc[:, 0].dropna().values, ps.iloc[:-1, 0].values |
| 52 | + ) |
| 53 | + |
| 54 | + shifted2 = ps.shift(1, "B") |
| 55 | + shifted3 = ps.shift(1, offsets.BDay()) |
| 56 | + tm.assert_frame_equal(shifted2, shifted3) |
| 57 | + tm.assert_frame_equal(ps, shifted2.shift(-1, "B")) |
| 58 | + |
| 59 | + msg = "does not match PeriodIndex freq" |
| 60 | + with pytest.raises(ValueError, match=msg): |
| 61 | + ps.shift(freq="D") |
| 62 | + |
| 63 | + # shift other axis |
| 64 | + # GH#6371 |
| 65 | + df = DataFrame(np.random.rand(10, 5)) |
| 66 | + expected = pd.concat( |
| 67 | + [DataFrame(np.nan, index=df.index, columns=[0]), df.iloc[:, 0:-1]], |
| 68 | + ignore_index=True, |
| 69 | + axis=1, |
| 70 | + ) |
| 71 | + result = df.shift(1, axis=1) |
| 72 | + tm.assert_frame_equal(result, expected) |
| 73 | + |
| 74 | + # shift named axis |
| 75 | + df = DataFrame(np.random.rand(10, 5)) |
| 76 | + expected = pd.concat( |
| 77 | + [DataFrame(np.nan, index=df.index, columns=[0]), df.iloc[:, 0:-1]], |
| 78 | + ignore_index=True, |
| 79 | + axis=1, |
| 80 | + ) |
| 81 | + result = df.shift(1, axis="columns") |
| 82 | + tm.assert_frame_equal(result, expected) |
| 83 | + |
| 84 | + def test_shift_bool(self): |
| 85 | + df = DataFrame({"high": [True, False], "low": [False, False]}) |
| 86 | + rs = df.shift(1) |
| 87 | + xp = DataFrame( |
| 88 | + np.array([[np.nan, np.nan], [True, False]], dtype=object), |
| 89 | + columns=["high", "low"], |
| 90 | + ) |
| 91 | + tm.assert_frame_equal(rs, xp) |
| 92 | + |
| 93 | + def test_shift_categorical(self): |
| 94 | + # GH#9416 |
| 95 | + s1 = pd.Series(["a", "b", "c"], dtype="category") |
| 96 | + s2 = pd.Series(["A", "B", "C"], dtype="category") |
| 97 | + df = DataFrame({"one": s1, "two": s2}) |
| 98 | + rs = df.shift(1) |
| 99 | + xp = DataFrame({"one": s1.shift(1), "two": s2.shift(1)}) |
| 100 | + tm.assert_frame_equal(rs, xp) |
| 101 | + |
| 102 | + def test_shift_fill_value(self): |
| 103 | + # GH#24128 |
| 104 | + df = DataFrame( |
| 105 | + [1, 2, 3, 4, 5], index=date_range("1/1/2000", periods=5, freq="H") |
| 106 | + ) |
| 107 | + exp = DataFrame( |
| 108 | + [0, 1, 2, 3, 4], index=date_range("1/1/2000", periods=5, freq="H") |
| 109 | + ) |
| 110 | + result = df.shift(1, fill_value=0) |
| 111 | + tm.assert_frame_equal(result, exp) |
| 112 | + |
| 113 | + exp = DataFrame( |
| 114 | + [0, 0, 1, 2, 3], index=date_range("1/1/2000", periods=5, freq="H") |
| 115 | + ) |
| 116 | + result = df.shift(2, fill_value=0) |
| 117 | + tm.assert_frame_equal(result, exp) |
| 118 | + |
| 119 | + def test_shift_empty(self): |
| 120 | + # Regression test for GH#8019 |
| 121 | + df = DataFrame({"foo": []}) |
| 122 | + rs = df.shift(-1) |
| 123 | + |
| 124 | + tm.assert_frame_equal(df, rs) |
| 125 | + |
| 126 | + def test_shift_duplicate_columns(self): |
| 127 | + # GH#9092; verify that position-based shifting works |
| 128 | + # in the presence of duplicate columns |
| 129 | + column_lists = [list(range(5)), [1] * 5, [1, 1, 2, 2, 1]] |
| 130 | + data = np.random.randn(20, 5) |
| 131 | + |
| 132 | + shifted = [] |
| 133 | + for columns in column_lists: |
| 134 | + df = pd.DataFrame(data.copy(), columns=columns) |
| 135 | + for s in range(5): |
| 136 | + df.iloc[:, s] = df.iloc[:, s].shift(s + 1) |
| 137 | + df.columns = range(5) |
| 138 | + shifted.append(df) |
| 139 | + |
| 140 | + # sanity check the base case |
| 141 | + nulls = shifted[0].isna().sum() |
| 142 | + tm.assert_series_equal(nulls, Series(range(1, 6), dtype="int64")) |
| 143 | + |
| 144 | + # check all answers are the same |
| 145 | + tm.assert_frame_equal(shifted[0], shifted[1]) |
| 146 | + tm.assert_frame_equal(shifted[0], shifted[2]) |
| 147 | + |
| 148 | + def test_tshift(self, datetime_frame): |
| 149 | + # PeriodIndex |
| 150 | + ps = tm.makePeriodFrame() |
| 151 | + shifted = ps.tshift(1) |
| 152 | + unshifted = shifted.tshift(-1) |
| 153 | + |
| 154 | + tm.assert_frame_equal(unshifted, ps) |
| 155 | + |
| 156 | + shifted2 = ps.tshift(freq="B") |
| 157 | + tm.assert_frame_equal(shifted, shifted2) |
| 158 | + |
| 159 | + shifted3 = ps.tshift(freq=offsets.BDay()) |
| 160 | + tm.assert_frame_equal(shifted, shifted3) |
| 161 | + |
| 162 | + with pytest.raises(ValueError, match="does not match"): |
| 163 | + ps.tshift(freq="M") |
| 164 | + |
| 165 | + # DatetimeIndex |
| 166 | + shifted = datetime_frame.tshift(1) |
| 167 | + unshifted = shifted.tshift(-1) |
| 168 | + |
| 169 | + tm.assert_frame_equal(datetime_frame, unshifted) |
| 170 | + |
| 171 | + shifted2 = datetime_frame.tshift(freq=datetime_frame.index.freq) |
| 172 | + tm.assert_frame_equal(shifted, shifted2) |
| 173 | + |
| 174 | + inferred_ts = DataFrame( |
| 175 | + datetime_frame.values, |
| 176 | + Index(np.asarray(datetime_frame.index)), |
| 177 | + columns=datetime_frame.columns, |
| 178 | + ) |
| 179 | + shifted = inferred_ts.tshift(1) |
| 180 | + unshifted = shifted.tshift(-1) |
| 181 | + tm.assert_frame_equal(shifted, datetime_frame.tshift(1)) |
| 182 | + tm.assert_frame_equal(unshifted, inferred_ts) |
| 183 | + |
| 184 | + no_freq = datetime_frame.iloc[[0, 5, 7], :] |
| 185 | + msg = "Freq was not given and was not set in the index" |
| 186 | + with pytest.raises(ValueError, match=msg): |
| 187 | + no_freq.tshift() |
0 commit comments