@@ -169,8 +169,18 @@ def test_transform_axis_1(request, transformation_func):
169169 msg = "DataFrame.groupby with axis=1 is deprecated"
170170 with tm .assert_produces_warning (FutureWarning , match = msg ):
171171 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
174184
175185 if transformation_func in ["diff" , "shift" ]:
176186 # Result contains nans, so transpose coerces to float
@@ -404,11 +414,25 @@ def mock_op(x):
404414 test_op = lambda x : x .transform (transformation_func )
405415 mock_op = lambda x : getattr (x , transformation_func )()
406416
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+
408428 # pass the group in same order as iterating `for ... in df.groupby(...)`
409429 # but reorder to match df's index since this is a transform
410430 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 ()
412436 # sort_index does not preserve the freq
413437 expected = expected .set_axis (df .index )
414438
@@ -969,14 +993,9 @@ def test_pct_change(frame_or_series, freq, periods, fill_method, limit):
969993 else :
970994 expected = expected .to_frame ("vals" )
971995
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
975998 )
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- )
980999 tm .assert_equal (result , expected )
9811000
9821001
@@ -1394,13 +1413,21 @@ def test_null_group_str_transformer(request, dropna, transformation_func):
13941413 args = get_groupby_method_args (transformation_func , df )
13951414 gb = df .groupby ("A" , dropna = dropna )
13961415
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+
13971421 buffer = []
13981422 for k , (idx , group ) in enumerate (gb ):
13991423 if transformation_func == "cumcount" :
14001424 # DataFrame has no cumcount method
14011425 res = DataFrame ({"B" : range (len (group ))}, index = group .index )
14021426 elif transformation_func == "ngroup" :
14031427 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 )
14041431 else :
14051432 res = getattr (group [["B" ]], transformation_func )(* args )
14061433 buffer .append (res )
@@ -1413,7 +1440,11 @@ def test_null_group_str_transformer(request, dropna, transformation_func):
14131440 # ngroup/cumcount always returns a Series as it counts the groups, not values
14141441 expected = expected ["B" ].rename (None )
14151442
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 )
14171448
14181449 tm .assert_equal (result , expected )
14191450
@@ -1465,13 +1496,21 @@ def test_null_group_str_transformer_series(dropna, transformation_func):
14651496 args = get_groupby_method_args (transformation_func , ser )
14661497 gb = ser .groupby ([1 , 1 , np .nan ], dropna = dropna )
14671498
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+
14681504 buffer = []
14691505 for k , (idx , group ) in enumerate (gb ):
14701506 if transformation_func == "cumcount" :
14711507 # Series has no cumcount method
14721508 res = Series (range (len (group )), index = group .index )
14731509 elif transformation_func == "ngroup" :
14741510 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 )
14751514 else :
14761515 res = getattr (group , transformation_func )(* args )
14771516 buffer .append (res )
@@ -1480,7 +1519,10 @@ def test_null_group_str_transformer_series(dropna, transformation_func):
14801519 buffer .append (Series ([np .nan ], index = [3 ], dtype = dtype ))
14811520 expected = concat (buffer )
14821521
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 :
14841526 result = gb .transform (transformation_func , * args )
14851527
14861528 tm .assert_equal (result , expected )
@@ -1523,6 +1565,14 @@ def test_as_index_no_change(keys, df, groupby_func):
15231565 args = get_groupby_method_args (groupby_func , df )
15241566 gb_as_index_true = df .groupby (keys , as_index = True )
15251567 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 )
15281578 tm .assert_equal (result , expected )
0 commit comments