11from collections import OrderedDict
22from datetime import datetime
33from itertools import chain
4- import operator
54import warnings
65
76import numpy as np
1413import pandas ._testing as tm
1514from pandas .core .apply import frame_apply
1615from pandas .core .base import SpecificationError
16+ from pandas .tests .frame .common import zip_frames
1717
1818
1919@pytest .fixture
@@ -1058,25 +1058,6 @@ def test_consistency_for_boxed(self, box, int_frame_const_col):
10581058 tm .assert_frame_equal (result , expected )
10591059
10601060
1061- def zip_frames (frames , axis = 1 ):
1062- """
1063- take a list of frames, zip them together under the
1064- assumption that these all have the first frames' index/columns.
1065-
1066- Returns
1067- -------
1068- new_frame : DataFrame
1069- """
1070- if axis == 1 :
1071- columns = frames [0 ].columns
1072- zipped = [f .loc [:, c ] for c in columns for f in frames ]
1073- return pd .concat (zipped , axis = 1 )
1074- else :
1075- index = frames [0 ].index
1076- zipped = [f .loc [i , :] for i in index for f in frames ]
1077- return pd .DataFrame (zipped )
1078-
1079-
10801061class TestDataFrameAggregate :
10811062 def test_agg_transform (self , axis , float_frame ):
10821063 other_axis = 1 if axis in {0 , "index" } else 0
@@ -1087,16 +1068,10 @@ def test_agg_transform(self, axis, float_frame):
10871068 f_sqrt = np .sqrt (float_frame )
10881069
10891070 # ufunc
1090- result = float_frame .transform (np .sqrt , axis = axis )
10911071 expected = f_sqrt .copy ()
1092- tm .assert_frame_equal (result , expected )
1093-
10941072 result = float_frame .apply (np .sqrt , axis = axis )
10951073 tm .assert_frame_equal (result , expected )
10961074
1097- result = float_frame .transform (np .sqrt , axis = axis )
1098- tm .assert_frame_equal (result , expected )
1099-
11001075 # list-like
11011076 result = float_frame .apply ([np .sqrt ], axis = axis )
11021077 expected = f_sqrt .copy ()
@@ -1110,9 +1085,6 @@ def test_agg_transform(self, axis, float_frame):
11101085 )
11111086 tm .assert_frame_equal (result , expected )
11121087
1113- result = float_frame .transform ([np .sqrt ], axis = axis )
1114- tm .assert_frame_equal (result , expected )
1115-
11161088 # multiple items in list
11171089 # these are in the order as if we are applying both
11181090 # functions per series and then concatting
@@ -1128,38 +1100,19 @@ def test_agg_transform(self, axis, float_frame):
11281100 )
11291101 tm .assert_frame_equal (result , expected )
11301102
1131- result = float_frame .transform ([np .abs , "sqrt" ], axis = axis )
1132- tm .assert_frame_equal (result , expected )
1133-
11341103 def test_transform_and_agg_err (self , axis , float_frame ):
11351104 # cannot both transform and agg
1136- msg = "transforms cannot produce aggregated results"
1137- with pytest .raises (ValueError , match = msg ):
1138- float_frame .transform (["max" , "min" ], axis = axis )
1139-
11401105 msg = "cannot combine transform and aggregation operations"
11411106 with pytest .raises (ValueError , match = msg ):
11421107 with np .errstate (all = "ignore" ):
11431108 float_frame .agg (["max" , "sqrt" ], axis = axis )
11441109
1145- with pytest .raises (ValueError , match = msg ):
1146- with np .errstate (all = "ignore" ):
1147- float_frame .transform (["max" , "sqrt" ], axis = axis )
1148-
11491110 df = pd .DataFrame ({"A" : range (5 ), "B" : 5 })
11501111
11511112 def f ():
11521113 with np .errstate (all = "ignore" ):
11531114 df .agg ({"A" : ["abs" , "sum" ], "B" : ["mean" , "max" ]}, axis = axis )
11541115
1155- @pytest .mark .parametrize ("method" , ["abs" , "shift" , "pct_change" , "cumsum" , "rank" ])
1156- def test_transform_method_name (self , method ):
1157- # GH 19760
1158- df = pd .DataFrame ({"A" : [- 1 , 2 ]})
1159- result = df .transform (method )
1160- expected = operator .methodcaller (method )(df )
1161- tm .assert_frame_equal (result , expected )
1162-
11631116 def test_demo (self ):
11641117 # demonstration tests
11651118 df = pd .DataFrame ({"A" : range (5 ), "B" : 5 })
0 commit comments