@@ -2078,11 +2078,7 @@ def fillna(self, value=None, method='pad', inplace=False,
2078
2078
if method is None : # pragma: no cover
2079
2079
raise ValueError ('must specify a fill method' )
2080
2080
2081
- method = com ._clean_fill_method (method )
2082
- if method == 'pad' :
2083
- fill_f = com .pad_1d
2084
- elif method == 'backfill' :
2085
- fill_f = com .backfill_1d
2081
+ fill_f = _get_fill_func (method )
2086
2082
2087
2083
if inplace :
2088
2084
values = self .values
@@ -2098,6 +2094,91 @@ def fillna(self, value=None, method='pad', inplace=False,
2098
2094
2099
2095
return result
2100
2096
2097
+
2098
+ def replace (self , to_replace = None , value = None , method = 'pad' , inplace = False ,
2099
+ limit = None ):
2100
+ """
2101
+ Replace arbitrary values in a Series
2102
+
2103
+ Parameters
2104
+ ----------
2105
+ to_replace : list or dict, default None
2106
+ list of values to be replaced or dict of replacement values
2107
+ value : anything
2108
+ if to_replace is a list then value is the replacement value
2109
+ method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad'
2110
+ Method to use for filling holes in reindexed Series
2111
+ pad / ffill: propagate last valid observation forward to next valid
2112
+ backfill / bfill: use NEXT valid observation to fill gap
2113
+ inplace : boolean, default False
2114
+ If True, fill the Series in place. Note: this will modify any other
2115
+ views on this Series, for example a column in a DataFrame. Returns
2116
+ a reference to the filled object, which is self if inplace=True
2117
+ limit : int, default None
2118
+ Maximum size gap to forward or backward fill
2119
+
2120
+ Notes
2121
+ -----
2122
+ replace does not distinguish between NaN and None
2123
+
2124
+ See also
2125
+ --------
2126
+ fillna, reindex, asfreq
2127
+
2128
+ Returns
2129
+ -------
2130
+ replaced : Series
2131
+ """
2132
+ result = self .copy () if not inplace else self
2133
+ single_val = False
2134
+
2135
+ def _rep_one (s , to_rep , v ): # replace single value
2136
+ m = _mask_missing (s , to_rep )
2137
+ np .putmask (s , m , v )
2138
+ return s
2139
+
2140
+ def _rep_dict (rs , to_rep ): # replace {[src] -> dest}
2141
+
2142
+ dd = {} # group by unique destination value
2143
+ [dd .setdefault (d , []).append (s ) for s , d in to_rep .iteritems ()]
2144
+
2145
+ for d , sset in dd .iteritems (): # now replace by each dest
2146
+ rs = _rep_one (rs , sset , d )
2147
+ return rs
2148
+
2149
+ if isinstance (to_replace , dict ):
2150
+ return _rep_dict (result , to_replace )
2151
+
2152
+ if isinstance (to_replace , (list , np .ndarray )):
2153
+
2154
+ if isinstance (value , (list , np .ndarray )): # check same length
2155
+
2156
+ vl , rl = len (value ), len (to_replace )
2157
+ if vl == rl :
2158
+ return _rep_dict (result , dict (zip (to_replace , value )))
2159
+ raise ValueError ('Got %d to replace but %d values' % (rl , vl ))
2160
+
2161
+ elif value is not None : # otherwise all replaced with same value
2162
+
2163
+ return _rep_one (result , to_replace , value )
2164
+
2165
+ else : # method
2166
+ if method is None : # pragma: no cover
2167
+ raise ValueError ('must specify a fill method' )
2168
+ fill_f = _get_fill_func (method )
2169
+
2170
+ mask = _mask_missing (result , to_replace )
2171
+ fill_f (result .values , limit = limit , mask = mask )
2172
+
2173
+ if not inplace :
2174
+ result = Series (result .values , index = self .index ,
2175
+ name = self .name )
2176
+ return result
2177
+
2178
+
2179
+ raise ValueError ('Unrecognized to_replace type %s' %
2180
+ type (to_replace ))
2181
+
2101
2182
def isin (self , values ):
2102
2183
"""
2103
2184
Return boolean vector showing whether each element in the Series is
@@ -2549,6 +2630,23 @@ def _resolve_offset(freq, kwds):
2549
2630
2550
2631
return offset
2551
2632
2633
+ def _get_fill_func (method ):
2634
+ method = com ._clean_fill_method (method )
2635
+ if method == 'pad' :
2636
+ fill_f = com .pad_1d
2637
+ elif method == 'backfill' :
2638
+ fill_f = com .backfill_1d
2639
+ return fill_f
2640
+
2641
+ def _mask_missing (series , missing_values ):
2642
+ missing_values = np .array (list (missing_values ), dtype = object )
2643
+ if isnull (missing_values ).any ():
2644
+ missing_values = missing_values [notnull (missing_values )]
2645
+ mask = isnull (series ) | series .isin (missing_values )
2646
+ else :
2647
+ mask = series .isin (missing_values )
2648
+ return mask
2649
+
2552
2650
2553
2651
#----------------------------------------------------------------------
2554
2652
# Add plotting methods to Series
0 commit comments