@@ -337,21 +337,19 @@ def test_upsample_sum(method, method_args, expected_values):
337
337
tm .assert_series_equal (result , expected )
338
338
339
339
340
- def test_groupby_resample_interpolate ():
341
- # GH 35325
342
- d = {"price" : [10 , 11 , 9 ], "volume" : [50 , 60 , 50 ]}
343
-
344
- df = DataFrame (d )
345
-
340
+ @pytest .fixture
341
+ def groupy_test_df ():
342
+ df = DataFrame ({"price" : [10 , 11 , 9 ], "volume" : [50 , 60 , 50 ]})
346
343
df ["week_starting" ] = date_range ("01/01/2018" , periods = 3 , freq = "W" )
344
+ return df .set_index ("week_starting" )
347
345
346
+
347
+ def test_groupby_resample_interpolate (groupy_test_df ):
348
+ # GH 35325
348
349
msg = "DataFrameGroupBy.resample operated on the grouping columns"
349
350
with tm .assert_produces_warning (FutureWarning , match = msg ):
350
351
result = (
351
- df .set_index ("week_starting" )
352
- .groupby ("volume" )
353
- .resample ("1D" )
354
- .interpolate (method = "linear" )
352
+ groupy_test_df .groupby ("volume" ).resample ("1D" ).interpolate (method = "linear" )
355
353
)
356
354
357
355
volume = [50 ] * 15 + [60 ]
@@ -388,3 +386,40 @@ def test_groupby_resample_interpolate():
388
386
index = expected_ind ,
389
387
)
390
388
tm .assert_frame_equal (result , expected )
389
+
390
+
391
+ def test_groupby_resample_interpolate_off_grid (groupy_test_df ):
392
+ """Similar test as test_groupby_resample_interpolate but with resampling
393
+ that results in missing anchor points when interpolating. See GH#21351."""
394
+ # GH#21351
395
+ msg = "DataFrameGroupBy.resample operated on the grouping columns"
396
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
397
+ result = (
398
+ groupy_test_df .groupby ("volume" )
399
+ .resample ("265H" )
400
+ .interpolate (method = "linear" )
401
+ )
402
+
403
+ volume = [50 , 50 , 60 ]
404
+ week_starting = [
405
+ Timestamp ("2018-01-07" ),
406
+ Timestamp ("2018-01-18 01:00:00" ),
407
+ Timestamp ("2018-01-14" ),
408
+ ]
409
+ expected_ind = pd .MultiIndex .from_arrays (
410
+ [volume , week_starting ],
411
+ names = ["volume" , "week_starting" ],
412
+ )
413
+
414
+ expected = DataFrame (
415
+ data = {
416
+ "price" : [
417
+ 10.0 ,
418
+ 9.5 ,
419
+ 11.0 ,
420
+ ],
421
+ "volume" : np .array (volume ).astype (float ),
422
+ },
423
+ index = expected_ind ,
424
+ )
425
+ tm .assert_frame_equal (result , expected )
0 commit comments