@@ -2186,7 +2186,8 @@ def _shift_indexer(self, periods):
2186
2186
#----------------------------------------------------------------------
2187
2187
# Function application
2188
2188
2189
- def apply (self , func , axis = 0 , broadcast = False , raw = False ):
2189
+ def apply (self , func , axis = 0 , broadcast = False , raw = False ,
2190
+ args = (), ** kwds ):
2190
2191
"""
2191
2192
Applies function along input axis of DataFrame. Objects passed to
2192
2193
functions are Series objects having index either the DataFrame's index
@@ -2207,6 +2208,10 @@ def apply(self, func, axis=0, broadcast=False, raw=False):
2207
2208
passed function will receive ndarray objects instead. If you are
2208
2209
just applying a NumPy reduction function this will achieve much
2209
2210
better performance
2211
+ args : tuple
2212
+ Positional arguments to pass to function in addition to the
2213
+ array/series
2214
+ Additional keyword arguments will be passed as keywords to the function
2210
2215
2211
2216
Examples
2212
2217
--------
@@ -2226,26 +2231,31 @@ def apply(self, func, axis=0, broadcast=False, raw=False):
2226
2231
if len (self .columns ) == 0 and len (self .index ) == 0 :
2227
2232
return self
2228
2233
2229
- if isinstance (func , np .ufunc ):
2230
- results = func (self .values )
2234
+ if kwds or args and not isinstance (func , np .ufunc ):
2235
+ f = lambda x : func (x , * args , ** kwds )
2236
+ else :
2237
+ f = func
2238
+
2239
+ if isinstance (f , np .ufunc ):
2240
+ results = f (self .values )
2231
2241
return self ._constructor (data = results , index = self .index ,
2232
2242
columns = self .columns , copy = False )
2233
2243
else :
2234
2244
if not broadcast :
2235
2245
if not all (self .shape ):
2236
- is_reduction = not isinstance (func (_EMPTY_SERIES ),
2246
+ is_reduction = not isinstance (f (_EMPTY_SERIES ),
2237
2247
np .ndarray )
2238
2248
if is_reduction :
2239
2249
return Series (np .nan , index = self ._get_agg_axis (axis ))
2240
2250
else :
2241
2251
return self .copy ()
2242
2252
2243
2253
if raw and not self ._is_mixed_type :
2244
- return self ._apply_raw (func , axis )
2254
+ return self ._apply_raw (f , axis )
2245
2255
else :
2246
- return self ._apply_standard (func , axis )
2256
+ return self ._apply_standard (f , axis )
2247
2257
else :
2248
- return self ._apply_broadcast (func , axis )
2258
+ return self ._apply_broadcast (f , axis )
2249
2259
2250
2260
def _apply_raw (self , func , axis ):
2251
2261
try :
@@ -2857,12 +2867,10 @@ def idxmin(self, axis=0, skipna=True):
2857
2867
-------
2858
2868
idxmin : Series
2859
2869
"""
2860
- values = self .values .copy ()
2861
- if skipna and not issubclass (values .dtype .type , np .integer ):
2862
- np .putmask (values , - np .isfinite (values ), np .inf )
2863
- argmin_index = self ._get_axis (axis )
2864
- return Series ([argmin_index [i ] for i in values .argmin (axis )],
2865
- index = self ._get_agg_axis (axis ))
2870
+ indices = nanops .nanargmin (self .values , axis = axis , skipna = skipna )
2871
+ index = self ._get_axis (axis )
2872
+ result = [index [i ] if i >= 0 else np .nan for i in indices ]
2873
+ return Series (result , index = self ._get_agg_axis (axis ))
2866
2874
2867
2875
def idxmax (self , axis = 0 , skipna = True ):
2868
2876
"""
@@ -2881,12 +2889,10 @@ def idxmax(self, axis=0, skipna=True):
2881
2889
-------
2882
2890
idxmax : Series
2883
2891
"""
2884
- values = self .values .copy ()
2885
- if skipna and not issubclass (values .dtype .type , np .integer ):
2886
- np .putmask (values , - np .isfinite (values ), - np .inf )
2887
- argmax_index = self ._get_axis (axis )
2888
- return Series ([argmax_index [i ] for i in values .argmax (axis )],
2889
- index = self ._get_agg_axis (axis ))
2892
+ indices = nanops .nanargmax (self .values , axis = axis , skipna = skipna )
2893
+ index = self ._get_axis (axis )
2894
+ result = [index [i ] if i >= 0 else np .nan for i in indices ]
2895
+ return Series (result , index = self ._get_agg_axis (axis ))
2890
2896
2891
2897
def _agg_by_level (self , name , axis = 0 , level = 0 , skipna = True ):
2892
2898
method = getattr (type (self ), name )
0 commit comments