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