1
1
from collections import OrderedDict
2
2
from datetime import datetime
3
3
from itertools import chain
4
- import operator
5
4
import warnings
6
5
7
6
import numpy as np
14
13
import pandas ._testing as tm
15
14
from pandas .core .apply import frame_apply
16
15
from pandas .core .base import SpecificationError
16
+ from pandas .tests .frame .common import zip_frames
17
17
18
18
19
19
@pytest .fixture
@@ -1058,25 +1058,6 @@ def test_consistency_for_boxed(self, box, int_frame_const_col):
1058
1058
tm .assert_frame_equal (result , expected )
1059
1059
1060
1060
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
-
1080
1061
class TestDataFrameAggregate :
1081
1062
def test_agg_transform (self , axis , float_frame ):
1082
1063
other_axis = 1 if axis in {0 , "index" } else 0
@@ -1087,16 +1068,10 @@ def test_agg_transform(self, axis, float_frame):
1087
1068
f_sqrt = np .sqrt (float_frame )
1088
1069
1089
1070
# ufunc
1090
- result = float_frame .transform (np .sqrt , axis = axis )
1091
1071
expected = f_sqrt .copy ()
1092
- tm .assert_frame_equal (result , expected )
1093
-
1094
1072
result = float_frame .apply (np .sqrt , axis = axis )
1095
1073
tm .assert_frame_equal (result , expected )
1096
1074
1097
- result = float_frame .transform (np .sqrt , axis = axis )
1098
- tm .assert_frame_equal (result , expected )
1099
-
1100
1075
# list-like
1101
1076
result = float_frame .apply ([np .sqrt ], axis = axis )
1102
1077
expected = f_sqrt .copy ()
@@ -1110,9 +1085,6 @@ def test_agg_transform(self, axis, float_frame):
1110
1085
)
1111
1086
tm .assert_frame_equal (result , expected )
1112
1087
1113
- result = float_frame .transform ([np .sqrt ], axis = axis )
1114
- tm .assert_frame_equal (result , expected )
1115
-
1116
1088
# multiple items in list
1117
1089
# these are in the order as if we are applying both
1118
1090
# functions per series and then concatting
@@ -1128,38 +1100,19 @@ def test_agg_transform(self, axis, float_frame):
1128
1100
)
1129
1101
tm .assert_frame_equal (result , expected )
1130
1102
1131
- result = float_frame .transform ([np .abs , "sqrt" ], axis = axis )
1132
- tm .assert_frame_equal (result , expected )
1133
-
1134
1103
def test_transform_and_agg_err (self , axis , float_frame ):
1135
1104
# 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
-
1140
1105
msg = "cannot combine transform and aggregation operations"
1141
1106
with pytest .raises (ValueError , match = msg ):
1142
1107
with np .errstate (all = "ignore" ):
1143
1108
float_frame .agg (["max" , "sqrt" ], axis = axis )
1144
1109
1145
- with pytest .raises (ValueError , match = msg ):
1146
- with np .errstate (all = "ignore" ):
1147
- float_frame .transform (["max" , "sqrt" ], axis = axis )
1148
-
1149
1110
df = pd .DataFrame ({"A" : range (5 ), "B" : 5 })
1150
1111
1151
1112
def f ():
1152
1113
with np .errstate (all = "ignore" ):
1153
1114
df .agg ({"A" : ["abs" , "sum" ], "B" : ["mean" , "max" ]}, axis = axis )
1154
1115
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
-
1163
1116
def test_demo (self ):
1164
1117
# demonstration tests
1165
1118
df = pd .DataFrame ({"A" : range (5 ), "B" : 5 })
0 commit comments