4
4
import numpy as np
5
5
6
6
from pandas .core .frame import DataFrame
7
- from pandas .core .generic import NDFrame , PandasObject
7
+ from pandas .core .generic import NDFrame
8
8
from pandas .core .index import Index , MultiIndex
9
9
from pandas .core .internals import BlockManager
10
10
from pandas .core .series import Series
@@ -446,12 +446,14 @@ def _wrap_applied_output(self, *args, **kwargs):
446
446
raise NotImplementedError
447
447
448
448
def _wrap_frames (self , keys , values , not_indexed_same = False ):
449
+ from pandas .tools .merge import concat , _concat_frames_hierarchical
450
+
449
451
if not_indexed_same :
450
452
result = _concat_frames_hierarchical (values , keys ,
451
453
self .groupings ,
452
454
axis = self .axis )
453
455
else :
454
- result = _concat_frames (values , self .obj .index )
456
+ result = concat (values , axis = 0 ). reindex ( self .obj .index )
455
457
456
458
return result
457
459
@@ -1115,6 +1117,8 @@ def transform(self, func, *args, **kwargs):
1115
1117
>>> grouped = df.groupby(lambda x: mapping[x])
1116
1118
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
1117
1119
"""
1120
+ import pandas .tools .merge as merge
1121
+
1118
1122
applied = []
1119
1123
1120
1124
obj = self ._obj_with_exclusions
@@ -1138,124 +1142,10 @@ def transform(self, func, *args, **kwargs):
1138
1142
else :
1139
1143
applied .append (res )
1140
1144
1141
- return _concat_frames (applied , obj .index , obj .columns ,
1142
- axis = self .axis )
1143
-
1144
- def _concat_frames (frames , index , columns = None , axis = 0 ):
1145
- if len (frames ) == 1 :
1146
- return frames [0 ]
1147
-
1148
- if axis == 0 :
1149
- new_index = _concat_indexes ([x .index for x in frames ])
1150
- if columns is None :
1151
- new_columns = frames [0 ].columns
1152
- else :
1153
- new_columns = columns
1154
- else :
1155
- new_columns = _concat_indexes ([x .columns for x in frames ])
1156
- new_index = index
1157
-
1158
- if frames [0 ]._is_mixed_type :
1159
- new_data = {}
1160
- for col in new_columns :
1161
- new_data [col ] = np .concatenate ([x [col ].values for x in frames ])
1162
- return DataFrame (new_data , index = new_index , columns = new_columns )
1163
- else :
1164
- new_values = np .concatenate ([x .values for x in frames ], axis = axis )
1165
- result = DataFrame (new_values , index = new_index , columns = new_columns )
1166
- return result .reindex (index = index , columns = columns )
1167
-
1168
- def _concat_indexes (indexes ):
1169
- return indexes [0 ].append (indexes [1 :])
1170
-
1171
- def _concat_frames_hierarchical (frames , keys , groupings , axis = 0 ):
1172
- if axis == 0 :
1173
- indexes = [x .index for x in frames ]
1174
- new_index = _make_concat_multiindex (indexes , keys , groupings )
1175
- new_columns = frames [0 ].columns
1176
- else :
1177
- all_columns = [x .columns for x in frames ]
1178
- new_columns = _make_concat_multiindex (all_columns , keys , groupings )
1179
- new_index = frames [0 ].index
1180
-
1181
- if frames [0 ]._is_mixed_type :
1182
- new_data = {}
1183
- for col in new_columns :
1184
- new_data [col ] = np .concatenate ([x [col ].values for x in frames ])
1185
- return DataFrame (new_data , index = new_index , columns = new_columns )
1186
- else :
1187
- new_values = np .concatenate ([x .values for x in frames ], axis = axis )
1188
- return DataFrame (new_values , index = new_index , columns = new_columns )
1189
-
1190
- def _make_concat_multiindex (indexes , keys , groupings ):
1191
- names = [ping .name for ping in groupings ]
1192
-
1193
- if not _all_indexes_same (indexes ):
1194
- label_list = []
1195
-
1196
- # things are potentially different sizes, so compute the exact labels
1197
- # for each level and pass those to MultiIndex.from_arrays
1198
- if len (groupings ) == 1 :
1199
- zipped = [keys ]
1200
- else :
1201
- zipped = zip (* keys )
1202
-
1203
- for hlevel in zipped :
1204
- to_concat = []
1205
- for k , index in zip (hlevel , indexes ):
1206
- to_concat .append (np .repeat (k , len (index )))
1207
- label_list .append (np .concatenate (to_concat ))
1208
-
1209
- concat_index = _concat_indexes (indexes )
1210
-
1211
- # these go at the end
1212
- if isinstance (concat_index , MultiIndex ):
1213
- for level in range (concat_index .nlevels ):
1214
- label_list .append (concat_index .get_level_values (level ))
1215
- else :
1216
- label_list .append (concat_index .values )
1217
-
1218
- consensus_name = indexes [0 ].names
1219
- for index in indexes [1 :]:
1220
- if index .names != consensus_name :
1221
- consensus_name = [None ] * index .nlevels
1222
- break
1223
- names .extend (consensus_name )
1224
-
1225
- return MultiIndex .from_arrays (label_list , names = names )
1226
-
1227
- new_index = indexes [0 ]
1228
- n = len (new_index )
1229
-
1230
- names .append (indexes [0 ].name )
1231
-
1232
- # do something a bit more speedy
1233
- levels = [ping .group_index for ping in groupings ]
1234
- levels .append (new_index )
1235
-
1236
- # construct labels
1237
- labels = []
1238
-
1239
- if len (groupings ) == 1 :
1240
- zipped = [keys ]
1241
- else :
1242
- zipped = zip (* keys )
1243
-
1244
- for hlevel , ping in zip (zipped , groupings ):
1245
- get_id = ping .reverse_ids .__getitem__
1246
- mapped = [get_id (x ) for x in hlevel ]
1247
- labels .append (np .repeat (mapped , n ))
1248
-
1249
- # last labels for the new level
1250
- labels .append (np .tile (np .arange (n ), len (indexes )))
1251
- return MultiIndex (levels = levels , labels = labels , names = names )
1252
-
1253
- def _all_indexes_same (indexes ):
1254
- first = indexes [0 ]
1255
- for index in indexes [1 :]:
1256
- if not first .equals (index ):
1257
- return False
1258
- return True
1145
+ concat_index = obj .columns if self .axis == 0 else obj .index
1146
+ concatenated = merge .concat (applied , join_index = concat_index ,
1147
+ axis = self .axis )
1148
+ return concatenated .reindex_like (obj )
1259
1149
1260
1150
class PanelGroupBy (GroupBy ):
1261
1151
0 commit comments