@@ -2594,108 +2594,6 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
25942594 else :
25952595 return self ._constructor (mapped , index = self .index , name = self .name )
25962596
2597- def replace (self , to_replace , value = None , method = 'pad' , inplace = False ,
2598- limit = None ):
2599- """
2600- Replace arbitrary values in a Series
2601-
2602- Parameters
2603- ----------
2604- to_replace : list or dict
2605- list of values to be replaced or dict of replacement values
2606- value : anything
2607- if to_replace is a list then value is the replacement value
2608- method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad'
2609- Method to use for filling holes in reindexed Series
2610- pad / ffill: propagate last valid observation forward to next valid
2611- backfill / bfill: use NEXT valid observation to fill gap
2612- inplace : boolean, default False
2613- If True, fill the Series in place. Note: this will modify any other
2614- views on this Series, for example a column in a DataFrame. Returns
2615- a reference to the filled object, which is self if inplace=True
2616- limit : int, default None
2617- Maximum size gap to forward or backward fill
2618-
2619- Notes
2620- -----
2621- replace does not distinguish between NaN and None
2622-
2623- See also
2624- --------
2625- fillna, reindex, asfreq
2626-
2627- Returns
2628- -------
2629- replaced : Series
2630- """
2631-
2632- if inplace :
2633- result = self
2634- change = self
2635- else :
2636- result = self .copy ()
2637- change = None
2638-
2639- def _rep_one (s , to_rep , v ): # replace single value
2640- mask = com .mask_missing (s .values , to_rep )
2641- com ._maybe_upcast_putmask (s .values , mask , v , change = change )
2642-
2643- def _rep_dict (rs , to_rep ): # replace {[src] -> dest}
2644-
2645- all_src = set ()
2646- dd = {} # group by unique destination value
2647- for s , d in to_rep .iteritems ():
2648- dd .setdefault (d , []).append (s )
2649- all_src .add (s )
2650-
2651- if any (d in all_src for d in dd .keys ()):
2652- # don't clobber each other at the cost of temporaries
2653- masks = {}
2654- for d , sset in dd .iteritems (): # now replace by each dest
2655- masks [d ] = com .mask_missing (rs .values , sset )
2656-
2657- for d , m in masks .iteritems ():
2658- com ._maybe_upcast_putmask (rs .values , m , d , change = change )
2659- else : # if no risk of clobbering then simple
2660- for d , sset in dd .iteritems ():
2661- _rep_one (rs , sset , d )
2662-
2663- if np .isscalar (to_replace ):
2664- to_replace = [to_replace ]
2665-
2666- if isinstance (to_replace , dict ):
2667- _rep_dict (result , to_replace )
2668- elif isinstance (to_replace , (list , pa .Array , Series )):
2669-
2670- # check same length
2671- if isinstance (value , (list , pa .Array , Series )):
2672- vl , rl = len (value ), len (to_replace )
2673- if vl == rl :
2674- _rep_dict (result , dict (zip (to_replace , value )))
2675- else :
2676- raise ValueError ('Got %d to replace but %d values'
2677- % (rl , vl ))
2678-
2679- elif value is not None : # otherwise all replaced with same value
2680- _rep_one (result , to_replace , value )
2681- else : # method
2682- if method is None : # pragma: no cover
2683- raise ValueError ('must specify a fill method' )
2684- fill_f = _get_fill_func (method )
2685-
2686- mask = com .mask_missing (result .values , to_replace )
2687- fill_f (result .values , limit = limit , mask = mask )
2688-
2689- if not inplace :
2690- result = Series (result .values , index = self .index ,
2691- name = self .name )
2692- else :
2693- raise ValueError ('Unrecognized to_replace type %s' %
2694- type (to_replace ))
2695-
2696- if not inplace :
2697- return result
2698-
26992597 def align (self , other , join = 'outer' , level = None , copy = True ,
27002598 fill_value = None , method = None , limit = None ):
27012599 """
0 commit comments