8
8
NaT ,
9
9
PeriodIndex ,
10
10
Series ,
11
+ TimedeltaIndex ,
11
12
)
12
13
import pandas ._testing as tm
13
14
from pandas .core .groupby .groupby import DataError
@@ -110,7 +111,17 @@ def test_resample_empty_series(freq, empty_series_dti, resample_method, request)
110
111
)
111
112
112
113
ser = empty_series_dti
113
- result = getattr (ser .resample (freq ), resample_method )()
114
+ if freq == "M" and isinstance (ser .index , TimedeltaIndex ):
115
+ msg = (
116
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
117
+ "e.g. '24H' or '3D', not <MonthEnd>"
118
+ )
119
+ with pytest .raises (ValueError , match = msg ):
120
+ ser .resample (freq )
121
+ return
122
+
123
+ rs = ser .resample (freq )
124
+ result = getattr (rs , resample_method )()
114
125
115
126
expected = ser .copy ()
116
127
expected .index = _asfreq_compat (ser .index , freq )
@@ -150,11 +161,23 @@ def test_resample_nat_index_series(request, freq, series, resample_method):
150
161
@pytest .mark .parametrize ("resample_method" , ["count" , "size" ])
151
162
def test_resample_count_empty_series (freq , empty_series_dti , resample_method ):
152
163
# GH28427
153
- result = getattr (empty_series_dti .resample (freq ), resample_method )()
164
+ ser = empty_series_dti
165
+ if freq == "M" and isinstance (ser .index , TimedeltaIndex ):
166
+ msg = (
167
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
168
+ "e.g. '24H' or '3D', not <MonthEnd>"
169
+ )
170
+ with pytest .raises (ValueError , match = msg ):
171
+ ser .resample (freq )
172
+ return
173
+
174
+ rs = ser .resample (freq )
175
+
176
+ result = getattr (rs , resample_method )()
154
177
155
- index = _asfreq_compat (empty_series_dti .index , freq )
178
+ index = _asfreq_compat (ser .index , freq )
156
179
157
- expected = Series ([], dtype = "int64" , index = index , name = empty_series_dti .name )
180
+ expected = Series ([], dtype = "int64" , index = index , name = ser .name )
158
181
159
182
tm .assert_series_equal (result , expected )
160
183
@@ -165,7 +188,17 @@ def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
165
188
# GH13212
166
189
df = empty_frame_dti
167
190
# count retains dimensions too
168
- result = getattr (df .resample (freq , group_keys = False ), resample_method )()
191
+ if freq == "M" and isinstance (df .index , TimedeltaIndex ):
192
+ msg = (
193
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
194
+ "e.g. '24H' or '3D', not <MonthEnd>"
195
+ )
196
+ with pytest .raises (ValueError , match = msg ):
197
+ df .resample (freq , group_keys = False )
198
+ return
199
+
200
+ rs = df .resample (freq , group_keys = False )
201
+ result = getattr (rs , resample_method )()
169
202
if resample_method != "size" :
170
203
expected = df .copy ()
171
204
else :
@@ -188,6 +221,15 @@ def test_resample_count_empty_dataframe(freq, empty_frame_dti):
188
221
189
222
empty_frame_dti ["a" ] = []
190
223
224
+ if freq == "M" and isinstance (empty_frame_dti .index , TimedeltaIndex ):
225
+ msg = (
226
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
227
+ "e.g. '24H' or '3D', not <MonthEnd>"
228
+ )
229
+ with pytest .raises (ValueError , match = msg ):
230
+ empty_frame_dti .resample (freq )
231
+ return
232
+
191
233
result = empty_frame_dti .resample (freq ).count ()
192
234
193
235
index = _asfreq_compat (empty_frame_dti .index , freq )
@@ -204,6 +246,15 @@ def test_resample_size_empty_dataframe(freq, empty_frame_dti):
204
246
205
247
empty_frame_dti ["a" ] = []
206
248
249
+ if freq == "M" and isinstance (empty_frame_dti .index , TimedeltaIndex ):
250
+ msg = (
251
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
252
+ "e.g. '24H' or '3D', not <MonthEnd>"
253
+ )
254
+ with pytest .raises (ValueError , match = msg ):
255
+ empty_frame_dti .resample (freq )
256
+ return
257
+
207
258
result = empty_frame_dti .resample (freq ).size ()
208
259
209
260
index = _asfreq_compat (empty_frame_dti .index , freq )
@@ -233,6 +284,16 @@ def test_resample_empty_dtypes(index, dtype, resample_method):
233
284
def test_apply_to_empty_series (empty_series_dti , freq ):
234
285
# GH 14313
235
286
ser = empty_series_dti
287
+
288
+ if freq == "M" and isinstance (empty_series_dti .index , TimedeltaIndex ):
289
+ msg = (
290
+ "Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
291
+ "e.g. '24H' or '3D', not <MonthEnd>"
292
+ )
293
+ with pytest .raises (ValueError , match = msg ):
294
+ empty_series_dti .resample (freq )
295
+ return
296
+
236
297
result = ser .resample (freq , group_keys = False ).apply (lambda x : 1 )
237
298
expected = ser .resample (freq ).apply (np .sum )
238
299
0 commit comments