|
232 | 232 |
|
233 | 233 | """
|
234 | 234 |
|
235 |
| -def _single_replace(self, to_replace, method, inplace, limit): |
236 |
| - if self.ndim != 1: |
237 |
| - raise TypeError('cannot replace {0} with method {1} on a {2}' |
238 |
| - .format(to_replace, method, type(self).__name__)) |
239 |
| - |
240 |
| - orig_dtype = self.dtype |
241 |
| - result = self if inplace else self.copy() |
242 |
| - fill_f = missing.get_fill_func(method) |
243 |
| - |
244 |
| - mask = missing.mask_missing(result.values, to_replace) |
245 |
| - values = fill_f(result.values, limit=limit, mask=mask) |
246 |
| - |
247 |
| - if values.dtype == orig_dtype and inplace: |
248 |
| - return |
249 |
| - |
250 |
| - result = pd.Series(values, index=self.index, |
251 |
| - dtype=self.dtype).__finalize__(self) |
252 |
| - |
253 |
| - if inplace: |
254 |
| - self._update_inplace(result._data) |
255 |
| - return |
256 |
| - |
257 |
| - return result |
258 |
| - |
259 | 235 | # -----------------------------------------------------------------------
|
260 | 236 | # DataFrame class
|
261 | 237 |
|
@@ -3142,8 +3118,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
|
3142 | 3118 |
|
3143 | 3119 | See Also
|
3144 | 3120 | --------
|
3145 |
| - :func:`DataFrame.fillna` : Fill NA/NaN values |
3146 |
| - :func:`DataFrame.where` : Replace values based on boolean condition |
| 3121 | + DataFrame.fillna : Fill NA/NaN values |
| 3122 | + DataFrame.where : Replace values based on boolean condition |
3147 | 3123 |
|
3148 | 3124 | Returns
|
3149 | 3125 | -------
|
@@ -3269,153 +3245,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
|
3269 | 3245 | the correct type for replacement.
|
3270 | 3246 |
|
3271 | 3247 | """
|
3272 |
| - inplace = validate_bool_kwarg(inplace, 'inplace') |
3273 |
| - if not is_bool(regex) and to_replace is not None: |
3274 |
| - raise AssertionError("'to_replace' must be 'None' if 'regex' is " |
3275 |
| - "not a bool") |
3276 |
| - if axis is not None: |
3277 |
| - warnings.warn('the "axis" argument is deprecated ' |
3278 |
| - 'and will be removed in' |
3279 |
| - 'v0.13; this argument has no effect') |
3280 |
| - |
3281 |
| - self._consolidate_inplace() |
3282 |
| - |
3283 |
| - if value is None: |
3284 |
| - # passing a single value that is scalar like |
3285 |
| - # when value is None (GH5319), for compat |
3286 |
| - if not is_dict_like(to_replace) and not is_dict_like(regex): |
3287 |
| - to_replace = [to_replace] |
3288 |
| - |
3289 |
| - if isinstance(to_replace, (tuple, list)): |
3290 |
| - return _single_replace(self, to_replace, method, inplace, |
3291 |
| - limit) |
3292 |
| - |
3293 |
| - if not is_dict_like(to_replace): |
3294 |
| - if not is_dict_like(regex): |
3295 |
| - raise TypeError('If "to_replace" and "value" are both None' |
3296 |
| - ' and "to_replace" is not a list, then ' |
3297 |
| - 'regex must be a mapping') |
3298 |
| - to_replace = regex |
3299 |
| - regex = True |
3300 |
| - |
3301 |
| - items = list(compat.iteritems(to_replace)) |
3302 |
| - keys, values = lzip(*items) or ([], []) |
3303 |
| - |
3304 |
| - are_mappings = [is_dict_like(v) for v in values] |
3305 |
| - |
3306 |
| - if any(are_mappings): |
3307 |
| - if not all(are_mappings): |
3308 |
| - raise TypeError("If a nested mapping is passed, all values" |
3309 |
| - " of the top level mapping must be " |
3310 |
| - "mappings") |
3311 |
| - # passed a nested dict/Series |
3312 |
| - to_rep_dict = {} |
3313 |
| - value_dict = {} |
3314 |
| - |
3315 |
| - for k, v in items: |
3316 |
| - keys, values = lzip(*v.items()) or ([], []) |
3317 |
| - if set(keys) & set(values): |
3318 |
| - raise ValueError("Replacement not allowed with " |
3319 |
| - "overlapping keys and values") |
3320 |
| - to_rep_dict[k] = list(keys) |
3321 |
| - value_dict[k] = list(values) |
3322 |
| - |
3323 |
| - to_replace, value = to_rep_dict, value_dict |
3324 |
| - else: |
3325 |
| - to_replace, value = keys, values |
3326 |
| - |
3327 |
| - return self.replace(to_replace, value, inplace=inplace, |
3328 |
| - limit=limit, regex=regex) |
3329 |
| - else: |
3330 |
| - |
3331 |
| - # need a non-zero len on all axes |
3332 |
| - for a in self._AXIS_ORDERS: |
3333 |
| - if not len(self._get_axis(a)): |
3334 |
| - return self |
3335 |
| - |
3336 |
| - new_data = self._data |
3337 |
| - if is_dict_like(to_replace): |
3338 |
| - if is_dict_like(value): # {'A' : NA} -> {'A' : 0} |
3339 |
| - res = self if inplace else self.copy() |
3340 |
| - for c, src in compat.iteritems(to_replace): |
3341 |
| - if c in value and c in self: |
3342 |
| - # object conversion is handled in |
3343 |
| - # series.replace which is called recursivelly |
3344 |
| - res[c] = res[c].replace(to_replace=src, |
3345 |
| - value=value[c], |
3346 |
| - inplace=False, |
3347 |
| - regex=regex) |
3348 |
| - return None if inplace else res |
3349 |
| - |
3350 |
| - # {'A': NA} -> 0 |
3351 |
| - elif not is_list_like(value): |
3352 |
| - keys = [(k, src) for k, src in compat.iteritems(to_replace) |
3353 |
| - if k in self] |
3354 |
| - keys_len = len(keys) - 1 |
3355 |
| - for i, (k, src) in enumerate(keys): |
3356 |
| - convert = i == keys_len |
3357 |
| - new_data = new_data.replace(to_replace=src, |
3358 |
| - value=value, |
3359 |
| - filter=[k], |
3360 |
| - inplace=inplace, |
3361 |
| - regex=regex, |
3362 |
| - convert=convert) |
3363 |
| - else: |
3364 |
| - raise TypeError('value argument must be scalar, dict, or ' |
3365 |
| - 'Series') |
3366 |
| - |
3367 |
| - elif is_list_like(to_replace): # [NA, ''] -> [0, 'missing'] |
3368 |
| - if is_list_like(value): |
3369 |
| - if len(to_replace) != len(value): |
3370 |
| - raise ValueError('Replacement lists must match ' |
3371 |
| - 'in length. Expecting %d got %d ' % |
3372 |
| - (len(to_replace), len(value))) |
3373 |
| - |
3374 |
| - new_data = self._data.replace_list(src_list=to_replace, |
3375 |
| - dest_list=value, |
3376 |
| - inplace=inplace, |
3377 |
| - regex=regex) |
3378 |
| - |
3379 |
| - else: # [NA, ''] -> 0 |
3380 |
| - new_data = self._data.replace(to_replace=to_replace, |
3381 |
| - value=value, inplace=inplace, |
3382 |
| - regex=regex) |
3383 |
| - elif to_replace is None: |
3384 |
| - if not (is_re_compilable(regex) or |
3385 |
| - is_list_like(regex) or is_dict_like(regex)): |
3386 |
| - raise TypeError("'regex' must be a string or a compiled " |
3387 |
| - "regular expression or a list or dict of " |
3388 |
| - "strings or regular expressions, you " |
3389 |
| - "passed a" |
3390 |
| - " {0!r}".format(type(regex).__name__)) |
3391 |
| - return self.replace(regex, value, inplace=inplace, limit=limit, |
3392 |
| - regex=True) |
3393 |
| - else: |
3394 |
| - |
3395 |
| - # dest iterable dict-like |
3396 |
| - if is_dict_like(value): # NA -> {'A' : 0, 'B' : -1} |
3397 |
| - new_data = self._data |
3398 |
| - |
3399 |
| - for k, v in compat.iteritems(value): |
3400 |
| - if k in self: |
3401 |
| - new_data = new_data.replace(to_replace=to_replace, |
3402 |
| - value=v, filter=[k], |
3403 |
| - inplace=inplace, |
3404 |
| - regex=regex) |
3405 |
| - |
3406 |
| - elif not is_list_like(value): # NA -> 0 |
3407 |
| - new_data = self._data.replace(to_replace=to_replace, |
3408 |
| - value=value, inplace=inplace, |
3409 |
| - regex=regex) |
3410 |
| - else: |
3411 |
| - msg = ('Invalid "to_replace" type: ' |
3412 |
| - '{0!r}').format(type(to_replace).__name__) |
3413 |
| - raise TypeError(msg) # pragma: no cover |
3414 |
| - |
3415 |
| - if inplace: |
3416 |
| - self._update_inplace(new_data) |
3417 |
| - else: |
3418 |
| - return self._constructor(new_data).__finalize__(self) |
| 3248 | + return super(DataFrame, self).replace(to_replace=to_replace, |
| 3249 | + value=value, inplace=inplace, limit=limit, regex=regex, |
| 3250 | + method=method, axis=axis) |
3419 | 3251 |
|
3420 | 3252 | @Appender(_shared_docs['shift'] % _shared_doc_kwargs)
|
3421 | 3253 | def shift(self, periods=1, freq=None, axis=0):
|
|
0 commit comments