@@ -2041,31 +2041,37 @@ def _get_group_keys(self):
2041
2041
self .levels ,
2042
2042
self .labels )
2043
2043
2044
- def _cython_apply (self , f , data , axis , ** kwargs ):
2045
- output = collections .OrderedDict ()
2046
- for col in data .columns :
2047
- if col in self .names :
2048
- output [col ] = data [col ].values
2049
- else :
2050
- # duplicative of _get_cython_function; needs refactor
2051
- dtype_str = data [col ].dtype .name
2052
- values = data [col ].values [:, None ]
2053
- func = afunc = self ._get_func (f ['name' ], dtype_str )
2054
- f = f .get ('f' )
2044
+ def _cython_apply (self , ftype , data , axis , ** kwargs ):
2045
+ def _generate_output (ser ):
2046
+ # duplicative of _get_cython_function; needs refactor
2047
+ dtype_str = ser .dtype .name
2048
+ values = ser .values [:, None ]
2049
+ func = afunc = self ._get_func (ftype ['name' ], dtype_str )
2050
+ f = ftype .get ('f' )
2055
2051
2056
- def wrapper (* args , ** kwargs ):
2057
- return f (afunc , * args , ** kwargs )
2052
+ def wrapper (* args , ** kwargs ):
2053
+ return f (afunc , * args , ** kwargs )
2058
2054
2059
- func = wrapper
2060
- labels , _ , _ = self .group_info
2055
+ func = wrapper
2056
+ labels , _ , _ = self .group_info
2057
+
2058
+ result = _maybe_fill (np .empty_like (values , dtype = dtype_str ),
2059
+ fill_value = np .nan )
2060
+ func (result , values , labels , ** kwargs )
2061
2061
2062
- result = _maybe_fill (np .empty_like (values , dtype = dtype_str ),
2063
- fill_value = np .nan )
2064
- func (result , values , labels , ** kwargs )
2065
- output [col ] = result [:, 0 ]
2062
+ return result [:, 0 ]
2066
2063
2067
- # Ugh
2068
- return DataFrame (output , index = data .index )
2064
+ # Using introspection to determine result; not ideal needs refactor
2065
+ if type (data ) is Series :
2066
+ return Series (_generate_output (data ), name = data .name )
2067
+ else :
2068
+ output = collections .OrderedDict ()
2069
+ for col in data .columns :
2070
+ if col in self .names :
2071
+ output [col ] = data [col ].values
2072
+ else :
2073
+ output [col ] = _generate_output (data [col ])
2074
+ return DataFrame (output , index = data .index )
2069
2075
2070
2076
def apply (self , f , data , axis = 0 ):
2071
2077
mutated = self .mutated
0 commit comments