@@ -169,8 +169,18 @@ def test_transform_axis_1(request, transformation_func):
169
169
msg = "DataFrame.groupby with axis=1 is deprecated"
170
170
with tm .assert_produces_warning (FutureWarning , match = msg ):
171
171
gb = df .groupby ([0 , 0 , 1 ], axis = 1 )
172
- result = gb .transform (transformation_func , * args )
173
- expected = df .T .groupby ([0 , 0 , 1 ]).transform (transformation_func , * args ).T
172
+
173
+ pct_change_msg = (
174
+ "The default fill_method='ffill' in DataFrameGroupBy.pct_change is deprecated"
175
+ )
176
+ if transformation_func == "pct_change" :
177
+ with tm .assert_produces_warning (FutureWarning , match = pct_change_msg ):
178
+ result = gb .transform ("pct_change" , * args )
179
+ with tm .assert_produces_warning (FutureWarning , match = pct_change_msg ):
180
+ expected = df .T .groupby ([0 , 0 , 1 ]).transform ("pct_change" , * args ).T
181
+ else :
182
+ result = gb .transform (transformation_func , * args )
183
+ expected = df .T .groupby ([0 , 0 , 1 ]).transform (transformation_func , * args ).T
174
184
175
185
if transformation_func in ["diff" , "shift" ]:
176
186
# Result contains nans, so transpose coerces to float
@@ -404,11 +414,25 @@ def mock_op(x):
404
414
test_op = lambda x : x .transform (transformation_func )
405
415
mock_op = lambda x : getattr (x , transformation_func )()
406
416
407
- result = test_op (df .groupby ("A" ))
417
+ msg = "The default fill_method='pad' in DataFrame.pct_change is deprecated"
418
+ groupby_msg = (
419
+ "The default fill_method='ffill' in DataFrameGroupBy.pct_change is deprecated"
420
+ )
421
+
422
+ if transformation_func == "pct_change" :
423
+ with tm .assert_produces_warning (FutureWarning , match = groupby_msg ):
424
+ result = test_op (df .groupby ("A" ))
425
+ else :
426
+ result = test_op (df .groupby ("A" ))
427
+
408
428
# pass the group in same order as iterating `for ... in df.groupby(...)`
409
429
# but reorder to match df's index since this is a transform
410
430
groups = [df [["B" ]].iloc [4 :6 ], df [["B" ]].iloc [6 :], df [["B" ]].iloc [:4 ]]
411
- expected = concat ([mock_op (g ) for g in groups ]).sort_index ()
431
+ if transformation_func == "pct_change" :
432
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
433
+ expected = concat ([mock_op (g ) for g in groups ]).sort_index ()
434
+ else :
435
+ expected = concat ([mock_op (g ) for g in groups ]).sort_index ()
412
436
# sort_index does not preserve the freq
413
437
expected = expected .set_axis (df .index )
414
438
@@ -969,14 +993,9 @@ def test_pct_change(frame_or_series, freq, periods, fill_method, limit):
969
993
else :
970
994
expected = expected .to_frame ("vals" )
971
995
972
- msg = (
973
- "The 'fill_method' and 'limit' keywords in "
974
- f"{ type (gb ).__name__ } .pct_change are deprecated"
996
+ result = gb .pct_change (
997
+ periods = periods , fill_method = fill_method , limit = limit , freq = freq
975
998
)
976
- with tm .assert_produces_warning (FutureWarning , match = msg ):
977
- result = gb .pct_change (
978
- periods = periods , fill_method = fill_method , limit = limit , freq = freq
979
- )
980
999
tm .assert_equal (result , expected )
981
1000
982
1001
@@ -1394,13 +1413,21 @@ def test_null_group_str_transformer(request, dropna, transformation_func):
1394
1413
args = get_groupby_method_args (transformation_func , df )
1395
1414
gb = df .groupby ("A" , dropna = dropna )
1396
1415
1416
+ msg = "The default fill_method='pad' in DataFrame.pct_change is deprecated"
1417
+ groupby_msg = (
1418
+ "The default fill_method='ffill' in DataFrameGroupBy.pct_change is deprecated"
1419
+ )
1420
+
1397
1421
buffer = []
1398
1422
for k , (idx , group ) in enumerate (gb ):
1399
1423
if transformation_func == "cumcount" :
1400
1424
# DataFrame has no cumcount method
1401
1425
res = DataFrame ({"B" : range (len (group ))}, index = group .index )
1402
1426
elif transformation_func == "ngroup" :
1403
1427
res = DataFrame (len (group ) * [k ], index = group .index , columns = ["B" ])
1428
+ elif transformation_func == "pct_change" :
1429
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
1430
+ res = getattr (group [["B" ]], "pct_change" )(* args )
1404
1431
else :
1405
1432
res = getattr (group [["B" ]], transformation_func )(* args )
1406
1433
buffer .append (res )
@@ -1413,7 +1440,11 @@ def test_null_group_str_transformer(request, dropna, transformation_func):
1413
1440
# ngroup/cumcount always returns a Series as it counts the groups, not values
1414
1441
expected = expected ["B" ].rename (None )
1415
1442
1416
- result = gb .transform (transformation_func , * args )
1443
+ if transformation_func == "pct_change" :
1444
+ with tm .assert_produces_warning (FutureWarning , match = groupby_msg ):
1445
+ result = gb .transform ("pct_change" , * args )
1446
+ else :
1447
+ result = gb .transform (transformation_func , * args )
1417
1448
1418
1449
tm .assert_equal (result , expected )
1419
1450
@@ -1465,13 +1496,21 @@ def test_null_group_str_transformer_series(dropna, transformation_func):
1465
1496
args = get_groupby_method_args (transformation_func , ser )
1466
1497
gb = ser .groupby ([1 , 1 , np .nan ], dropna = dropna )
1467
1498
1499
+ msg = "The default fill_method='pad' in Series.pct_change is deprecated"
1500
+ groupby_msg = (
1501
+ "The default fill_method='ffill' in SeriesGroupBy.pct_change is deprecated"
1502
+ )
1503
+
1468
1504
buffer = []
1469
1505
for k , (idx , group ) in enumerate (gb ):
1470
1506
if transformation_func == "cumcount" :
1471
1507
# Series has no cumcount method
1472
1508
res = Series (range (len (group )), index = group .index )
1473
1509
elif transformation_func == "ngroup" :
1474
1510
res = Series (k , index = group .index )
1511
+ elif transformation_func == "pct_change" :
1512
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
1513
+ res = getattr (group , "pct_change" )(* args )
1475
1514
else :
1476
1515
res = getattr (group , transformation_func )(* args )
1477
1516
buffer .append (res )
@@ -1480,7 +1519,10 @@ def test_null_group_str_transformer_series(dropna, transformation_func):
1480
1519
buffer .append (Series ([np .nan ], index = [3 ], dtype = dtype ))
1481
1520
expected = concat (buffer )
1482
1521
1483
- with tm .assert_produces_warning (None ):
1522
+ if transformation_func == "pct_change" :
1523
+ with tm .assert_produces_warning (FutureWarning , match = groupby_msg ):
1524
+ result = gb .transform ("pct_change" , * args )
1525
+ else :
1484
1526
result = gb .transform (transformation_func , * args )
1485
1527
1486
1528
tm .assert_equal (result , expected )
@@ -1523,6 +1565,14 @@ def test_as_index_no_change(keys, df, groupby_func):
1523
1565
args = get_groupby_method_args (groupby_func , df )
1524
1566
gb_as_index_true = df .groupby (keys , as_index = True )
1525
1567
gb_as_index_false = df .groupby (keys , as_index = False )
1526
- result = gb_as_index_true .transform (groupby_func , * args )
1527
- expected = gb_as_index_false .transform (groupby_func , * args )
1568
+
1569
+ msg = "The default fill_method='ffill' in DataFrameGroupBy.pct_change is deprecated"
1570
+ if groupby_func == "pct_change" :
1571
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
1572
+ result = gb_as_index_true .transform ("pct_change" , * args )
1573
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
1574
+ expected = gb_as_index_false .transform ("pct_change" , * args )
1575
+ else :
1576
+ result = gb_as_index_true .transform (groupby_func , * args )
1577
+ expected = gb_as_index_false .transform (groupby_func , * args )
1528
1578
tm .assert_equal (result , expected )
0 commit comments