@@ -20,10 +20,10 @@ class TestDatetimeIndexShift:
20
20
# -------------------------------------------------------------
21
21
# DatetimeIndex.shift is used in integer addition
22
22
23
- def test_dti_shift_tzaware (self , tz_naive_fixture ):
23
+ def test_dti_shift_tzaware (self , tz_naive_fixture , unit ):
24
24
# GH#9903
25
25
tz = tz_naive_fixture
26
- idx = DatetimeIndex ([], name = "xxx" , tz = tz )
26
+ idx = DatetimeIndex ([], name = "xxx" , tz = tz ). as_unit ( unit )
27
27
tm .assert_index_equal (idx .shift (0 , freq = "h" ), idx )
28
28
tm .assert_index_equal (idx .shift (3 , freq = "h" ), idx )
29
29
@@ -32,50 +32,53 @@ def test_dti_shift_tzaware(self, tz_naive_fixture):
32
32
name = "xxx" ,
33
33
tz = tz ,
34
34
freq = "h" ,
35
- )
35
+ ). as_unit ( unit )
36
36
tm .assert_index_equal (idx .shift (0 , freq = "h" ), idx )
37
37
exp = DatetimeIndex (
38
38
["2011-01-01 13:00" , "2011-01-01 14:00" , "2011-01-01 15:00" ],
39
39
name = "xxx" ,
40
40
tz = tz ,
41
41
freq = "h" ,
42
- )
42
+ ). as_unit ( unit )
43
43
tm .assert_index_equal (idx .shift (3 , freq = "h" ), exp )
44
44
exp = DatetimeIndex (
45
45
["2011-01-01 07:00" , "2011-01-01 08:00" , "2011-01-01 09:00" ],
46
46
name = "xxx" ,
47
47
tz = tz ,
48
48
freq = "h" ,
49
- )
49
+ ). as_unit ( unit )
50
50
tm .assert_index_equal (idx .shift (- 3 , freq = "h" ), exp )
51
51
52
- def test_dti_shift_freqs (self ):
52
+ def test_dti_shift_freqs (self , unit ):
53
53
# test shift for DatetimeIndex and non DatetimeIndex
54
54
# GH#8083
55
- drange = date_range ("20130101" , periods = 5 )
55
+ drange = date_range ("20130101" , periods = 5 , unit = unit )
56
56
result = drange .shift (1 )
57
57
expected = DatetimeIndex (
58
58
["2013-01-02" , "2013-01-03" , "2013-01-04" , "2013-01-05" , "2013-01-06" ],
59
+ dtype = f"M8[{ unit } ]" ,
59
60
freq = "D" ,
60
61
)
61
62
tm .assert_index_equal (result , expected )
62
63
63
64
result = drange .shift (- 1 )
64
65
expected = DatetimeIndex (
65
66
["2012-12-31" , "2013-01-01" , "2013-01-02" , "2013-01-03" , "2013-01-04" ],
67
+ dtype = f"M8[{ unit } ]" ,
66
68
freq = "D" ,
67
69
)
68
70
tm .assert_index_equal (result , expected )
69
71
70
72
result = drange .shift (3 , freq = "2D" )
71
73
expected = DatetimeIndex (
72
74
["2013-01-07" , "2013-01-08" , "2013-01-09" , "2013-01-10" , "2013-01-11" ],
75
+ dtype = f"M8[{ unit } ]" ,
73
76
freq = "D" ,
74
77
)
75
78
tm .assert_index_equal (result , expected )
76
79
77
- def test_dti_shift_int (self ):
78
- rng = date_range ("1/1/2000" , periods = 20 )
80
+ def test_dti_shift_int (self , unit ):
81
+ rng = date_range ("1/1/2000" , periods = 20 , unit = unit )
79
82
80
83
result = rng + 5 * rng .freq
81
84
expected = rng .shift (5 )
@@ -85,25 +88,27 @@ def test_dti_shift_int(self):
85
88
expected = rng .shift (- 5 )
86
89
tm .assert_index_equal (result , expected )
87
90
88
- def test_dti_shift_no_freq (self ):
91
+ def test_dti_shift_no_freq (self , unit ):
89
92
# GH#19147
90
- dti = DatetimeIndex (["2011-01-01 10:00" , "2011-01-01" ], freq = None )
93
+ dti = DatetimeIndex (["2011-01-01 10:00" , "2011-01-01" ], freq = None ). as_unit ( unit )
91
94
with pytest .raises (NullFrequencyError , match = "Cannot shift with no freq" ):
92
95
dti .shift (2 )
93
96
94
97
@pytest .mark .parametrize ("tzstr" , ["US/Eastern" , "dateutil/US/Eastern" ])
95
- def test_dti_shift_localized (self , tzstr ):
96
- dr = date_range ("2011/1/1" , "2012/1/1" , freq = "W-FRI" )
98
+ def test_dti_shift_localized (self , tzstr , unit ):
99
+ dr = date_range ("2011/1/1" , "2012/1/1" , freq = "W-FRI" , unit = unit )
97
100
dr_tz = dr .tz_localize (tzstr )
98
101
99
102
result = dr_tz .shift (1 , "10min" )
100
103
assert result .tz == dr_tz .tz
101
104
102
- def test_dti_shift_across_dst (self ):
105
+ def test_dti_shift_across_dst (self , unit ):
103
106
# GH 8616
104
- idx = date_range ("2013-11-03" , tz = "America/Chicago" , periods = 7 , freq = "h" )
105
- s = Series (index = idx [:- 1 ], dtype = object )
106
- result = s .shift (freq = "h" )
107
+ idx = date_range (
108
+ "2013-11-03" , tz = "America/Chicago" , periods = 7 , freq = "h" , unit = unit
109
+ )
110
+ ser = Series (index = idx [:- 1 ], dtype = object )
111
+ result = ser .shift (freq = "h" )
107
112
expected = Series (index = idx [1 :], dtype = object )
108
113
tm .assert_series_equal (result , expected )
109
114
@@ -115,24 +120,26 @@ def test_dti_shift_across_dst(self):
115
120
[1 , "2014-11-14 01:00:00" ],
116
121
],
117
122
)
118
- def test_dti_shift_near_midnight (self , shift , result_time ):
123
+ def test_dti_shift_near_midnight (self , shift , result_time , unit ):
119
124
# GH 8616
120
125
dt = datetime (2014 , 11 , 14 , 0 )
121
126
dt_est = pytz .timezone ("EST" ).localize (dt )
122
- s = Series (data = [1 ], index = [dt_est ])
123
- result = s .shift (shift , freq = "h" )
124
- expected = Series (1 , index = DatetimeIndex ([result_time ], tz = "EST" ))
127
+ idx = DatetimeIndex ([dt_est ]).as_unit (unit )
128
+ ser = Series (data = [1 ], index = idx )
129
+ result = ser .shift (shift , freq = "h" )
130
+ exp_index = DatetimeIndex ([result_time ], tz = "EST" ).as_unit (unit )
131
+ expected = Series (1 , index = exp_index )
125
132
tm .assert_series_equal (result , expected )
126
133
127
- def test_shift_periods (self ):
134
+ def test_shift_periods (self , unit ):
128
135
# GH#22458 : argument 'n' was deprecated in favor of 'periods'
129
- idx = date_range (start = START , end = END , periods = 3 )
136
+ idx = date_range (start = START , end = END , periods = 3 , unit = unit )
130
137
tm .assert_index_equal (idx .shift (periods = 0 ), idx )
131
138
tm .assert_index_equal (idx .shift (0 ), idx )
132
139
133
140
@pytest .mark .parametrize ("freq" , ["B" , "C" ])
134
- def test_shift_bday (self , freq ):
135
- rng = date_range (START , END , freq = freq )
141
+ def test_shift_bday (self , freq , unit ):
142
+ rng = date_range (START , END , freq = freq , unit = unit )
136
143
shifted = rng .shift (5 )
137
144
assert shifted [0 ] == rng [5 ]
138
145
assert shifted .freq == rng .freq
@@ -145,18 +152,18 @@ def test_shift_bday(self, freq):
145
152
assert shifted [0 ] == rng [0 ]
146
153
assert shifted .freq == rng .freq
147
154
148
- def test_shift_bmonth (self ):
149
- rng = date_range (START , END , freq = pd .offsets .BMonthEnd ())
155
+ def test_shift_bmonth (self , unit ):
156
+ rng = date_range (START , END , freq = pd .offsets .BMonthEnd (), unit = unit )
150
157
shifted = rng .shift (1 , freq = pd .offsets .BDay ())
151
158
assert shifted [0 ] == rng [0 ] + pd .offsets .BDay ()
152
159
153
- rng = date_range (START , END , freq = pd .offsets .BMonthEnd ())
160
+ rng = date_range (START , END , freq = pd .offsets .BMonthEnd (), unit = unit )
154
161
with tm .assert_produces_warning (pd .errors .PerformanceWarning ):
155
162
shifted = rng .shift (1 , freq = pd .offsets .CDay ())
156
163
assert shifted [0 ] == rng [0 ] + pd .offsets .CDay ()
157
164
158
- def test_shift_empty (self ):
165
+ def test_shift_empty (self , unit ):
159
166
# GH#14811
160
- dti = date_range (start = "2016-10-21" , end = "2016-10-21" , freq = "BME" )
167
+ dti = date_range (start = "2016-10-21" , end = "2016-10-21" , freq = "BME" , unit = unit )
161
168
result = dti .shift (1 )
162
169
tm .assert_index_equal (result , dti )
0 commit comments