|
238 | 238 |
|
239 | 239 | """
|
240 | 240 |
|
241 |
| -def _single_replace(self, to_replace, method, inplace, limit): |
242 |
| - if self.ndim != 1: |
243 |
| - raise TypeError('cannot replace {0} with method {1} on a {2}' |
244 |
| - .format(to_replace, method, type(self).__name__)) |
245 |
| - |
246 |
| - orig_dtype = self.dtype |
247 |
| - result = self if inplace else self.copy() |
248 |
| - fill_f = missing.get_fill_func(method) |
249 |
| - |
250 |
| - mask = missing.mask_missing(result.values, to_replace) |
251 |
| - values = fill_f(result.values, limit=limit, mask=mask) |
252 |
| - |
253 |
| - if values.dtype == orig_dtype and inplace: |
254 |
| - return |
255 |
| - |
256 |
| - result = pd.Series(values, index=self.index, |
257 |
| - dtype=self.dtype).__finalize__(self) |
258 |
| - |
259 |
| - if inplace: |
260 |
| - self._update_inplace(result._data) |
261 |
| - return |
262 |
| - |
263 |
| - return result |
264 |
| - |
265 | 241 | # -----------------------------------------------------------------------
|
266 | 242 | # DataFrame class
|
267 | 243 |
|
@@ -3172,8 +3148,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
|
3172 | 3148 |
|
3173 | 3149 | See Also
|
3174 | 3150 | --------
|
3175 |
| - :func:`DataFrame.fillna` : Fill NA/NaN values |
3176 |
| - :func:`DataFrame.where` : Replace values based on boolean condition |
| 3151 | + DataFrame.fillna : Fill NA/NaN values |
| 3152 | + DataFrame.where : Replace values based on boolean condition |
3177 | 3153 |
|
3178 | 3154 | Returns
|
3179 | 3155 | -------
|
@@ -3299,153 +3275,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
|
3299 | 3275 | the correct type for replacement.
|
3300 | 3276 |
|
3301 | 3277 | """
|
3302 |
| - inplace = validate_bool_kwarg(inplace, 'inplace') |
3303 |
| - if not is_bool(regex) and to_replace is not None: |
3304 |
| - raise AssertionError("'to_replace' must be 'None' if 'regex' is " |
3305 |
| - "not a bool") |
3306 |
| - if axis is not None: |
3307 |
| - warnings.warn('the "axis" argument is deprecated ' |
3308 |
| - 'and will be removed in' |
3309 |
| - 'v0.13; this argument has no effect') |
3310 |
| - |
3311 |
| - self._consolidate_inplace() |
3312 |
| - |
3313 |
| - if value is None: |
3314 |
| - # passing a single value that is scalar like |
3315 |
| - # when value is None (GH5319), for compat |
3316 |
| - if not is_dict_like(to_replace) and not is_dict_like(regex): |
3317 |
| - to_replace = [to_replace] |
3318 |
| - |
3319 |
| - if isinstance(to_replace, (tuple, list)): |
3320 |
| - return _single_replace(self, to_replace, method, inplace, |
3321 |
| - limit) |
3322 |
| - |
3323 |
| - if not is_dict_like(to_replace): |
3324 |
| - if not is_dict_like(regex): |
3325 |
| - raise TypeError('If "to_replace" and "value" are both None' |
3326 |
| - ' and "to_replace" is not a list, then ' |
3327 |
| - 'regex must be a mapping') |
3328 |
| - to_replace = regex |
3329 |
| - regex = True |
3330 |
| - |
3331 |
| - items = list(compat.iteritems(to_replace)) |
3332 |
| - keys, values = lzip(*items) or ([], []) |
3333 |
| - |
3334 |
| - are_mappings = [is_dict_like(v) for v in values] |
3335 |
| - |
3336 |
| - if any(are_mappings): |
3337 |
| - if not all(are_mappings): |
3338 |
| - raise TypeError("If a nested mapping is passed, all values" |
3339 |
| - " of the top level mapping must be " |
3340 |
| - "mappings") |
3341 |
| - # passed a nested dict/Series |
3342 |
| - to_rep_dict = {} |
3343 |
| - value_dict = {} |
3344 |
| - |
3345 |
| - for k, v in items: |
3346 |
| - keys, values = lzip(*v.items()) or ([], []) |
3347 |
| - if set(keys) & set(values): |
3348 |
| - raise ValueError("Replacement not allowed with " |
3349 |
| - "overlapping keys and values") |
3350 |
| - to_rep_dict[k] = list(keys) |
3351 |
| - value_dict[k] = list(values) |
3352 |
| - |
3353 |
| - to_replace, value = to_rep_dict, value_dict |
3354 |
| - else: |
3355 |
| - to_replace, value = keys, values |
3356 |
| - |
3357 |
| - return self.replace(to_replace, value, inplace=inplace, |
3358 |
| - limit=limit, regex=regex) |
3359 |
| - else: |
3360 |
| - |
3361 |
| - # need a non-zero len on all axes |
3362 |
| - for a in self._AXIS_ORDERS: |
3363 |
| - if not len(self._get_axis(a)): |
3364 |
| - return self |
3365 |
| - |
3366 |
| - new_data = self._data |
3367 |
| - if is_dict_like(to_replace): |
3368 |
| - if is_dict_like(value): # {'A' : NA} -> {'A' : 0} |
3369 |
| - res = self if inplace else self.copy() |
3370 |
| - for c, src in compat.iteritems(to_replace): |
3371 |
| - if c in value and c in self: |
3372 |
| - # object conversion is handled in |
3373 |
| - # series.replace which is called recursivelly |
3374 |
| - res[c] = res[c].replace(to_replace=src, |
3375 |
| - value=value[c], |
3376 |
| - inplace=False, |
3377 |
| - regex=regex) |
3378 |
| - return None if inplace else res |
3379 |
| - |
3380 |
| - # {'A': NA} -> 0 |
3381 |
| - elif not is_list_like(value): |
3382 |
| - keys = [(k, src) for k, src in compat.iteritems(to_replace) |
3383 |
| - if k in self] |
3384 |
| - keys_len = len(keys) - 1 |
3385 |
| - for i, (k, src) in enumerate(keys): |
3386 |
| - convert = i == keys_len |
3387 |
| - new_data = new_data.replace(to_replace=src, |
3388 |
| - value=value, |
3389 |
| - filter=[k], |
3390 |
| - inplace=inplace, |
3391 |
| - regex=regex, |
3392 |
| - convert=convert) |
3393 |
| - else: |
3394 |
| - raise TypeError('value argument must be scalar, dict, or ' |
3395 |
| - 'Series') |
3396 |
| - |
3397 |
| - elif is_list_like(to_replace): # [NA, ''] -> [0, 'missing'] |
3398 |
| - if is_list_like(value): |
3399 |
| - if len(to_replace) != len(value): |
3400 |
| - raise ValueError('Replacement lists must match ' |
3401 |
| - 'in length. Expecting %d got %d ' % |
3402 |
| - (len(to_replace), len(value))) |
3403 |
| - |
3404 |
| - new_data = self._data.replace_list(src_list=to_replace, |
3405 |
| - dest_list=value, |
3406 |
| - inplace=inplace, |
3407 |
| - regex=regex) |
3408 |
| - |
3409 |
| - else: # [NA, ''] -> 0 |
3410 |
| - new_data = self._data.replace(to_replace=to_replace, |
3411 |
| - value=value, inplace=inplace, |
3412 |
| - regex=regex) |
3413 |
| - elif to_replace is None: |
3414 |
| - if not (is_re_compilable(regex) or |
3415 |
| - is_list_like(regex) or is_dict_like(regex)): |
3416 |
| - raise TypeError("'regex' must be a string or a compiled " |
3417 |
| - "regular expression or a list or dict of " |
3418 |
| - "strings or regular expressions, you " |
3419 |
| - "passed a" |
3420 |
| - " {0!r}".format(type(regex).__name__)) |
3421 |
| - return self.replace(regex, value, inplace=inplace, limit=limit, |
3422 |
| - regex=True) |
3423 |
| - else: |
3424 |
| - |
3425 |
| - # dest iterable dict-like |
3426 |
| - if is_dict_like(value): # NA -> {'A' : 0, 'B' : -1} |
3427 |
| - new_data = self._data |
3428 |
| - |
3429 |
| - for k, v in compat.iteritems(value): |
3430 |
| - if k in self: |
3431 |
| - new_data = new_data.replace(to_replace=to_replace, |
3432 |
| - value=v, filter=[k], |
3433 |
| - inplace=inplace, |
3434 |
| - regex=regex) |
3435 |
| - |
3436 |
| - elif not is_list_like(value): # NA -> 0 |
3437 |
| - new_data = self._data.replace(to_replace=to_replace, |
3438 |
| - value=value, inplace=inplace, |
3439 |
| - regex=regex) |
3440 |
| - else: |
3441 |
| - msg = ('Invalid "to_replace" type: ' |
3442 |
| - '{0!r}').format(type(to_replace).__name__) |
3443 |
| - raise TypeError(msg) # pragma: no cover |
3444 |
| - |
3445 |
| - if inplace: |
3446 |
| - self._update_inplace(new_data) |
3447 |
| - else: |
3448 |
| - return self._constructor(new_data).__finalize__(self) |
| 3278 | + return super(DataFrame, self).replace(to_replace=to_replace, |
| 3279 | + value=value, inplace=inplace, limit=limit, regex=regex, |
| 3280 | + method=method, axis=axis) |
3449 | 3281 |
|
3450 | 3282 | @Appender(_shared_docs['shift'] % _shared_doc_kwargs)
|
3451 | 3283 | def shift(self, periods=1, freq=None, axis=0):
|
|
0 commit comments