88 NaT ,
99 PeriodIndex ,
1010 Series ,
11+ TimedeltaIndex ,
1112)
1213import pandas ._testing as tm
1314from pandas .core .groupby .groupby import DataError
@@ -110,7 +111,17 @@ def test_resample_empty_series(freq, empty_series_dti, resample_method, request)
110111 )
111112
112113 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 )()
114125
115126 expected = ser .copy ()
116127 expected .index = _asfreq_compat (ser .index , freq )
@@ -150,11 +161,23 @@ def test_resample_nat_index_series(request, freq, series, resample_method):
150161@pytest .mark .parametrize ("resample_method" , ["count" , "size" ])
151162def test_resample_count_empty_series (freq , empty_series_dti , resample_method ):
152163 # 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 )()
154177
155- index = _asfreq_compat (empty_series_dti .index , freq )
178+ index = _asfreq_compat (ser .index , freq )
156179
157- expected = Series ([], dtype = "int64" , index = index , name = empty_series_dti .name )
180+ expected = Series ([], dtype = "int64" , index = index , name = ser .name )
158181
159182 tm .assert_series_equal (result , expected )
160183
@@ -165,7 +188,17 @@ def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
165188 # GH13212
166189 df = empty_frame_dti
167190 # 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 )()
169202 if resample_method != "size" :
170203 expected = df .copy ()
171204 else :
@@ -188,6 +221,15 @@ def test_resample_count_empty_dataframe(freq, empty_frame_dti):
188221
189222 empty_frame_dti ["a" ] = []
190223
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+
191233 result = empty_frame_dti .resample (freq ).count ()
192234
193235 index = _asfreq_compat (empty_frame_dti .index , freq )
@@ -204,6 +246,15 @@ def test_resample_size_empty_dataframe(freq, empty_frame_dti):
204246
205247 empty_frame_dti ["a" ] = []
206248
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+
207258 result = empty_frame_dti .resample (freq ).size ()
208259
209260 index = _asfreq_compat (empty_frame_dti .index , freq )
@@ -233,6 +284,16 @@ def test_resample_empty_dtypes(index, dtype, resample_method):
233284def test_apply_to_empty_series (empty_series_dti , freq ):
234285 # GH 14313
235286 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+
236297 result = ser .resample (freq , group_keys = False ).apply (lambda x : 1 )
237298 expected = ser .resample (freq ).apply (np .sum )
238299
0 commit comments