Skip to content

Commit 045da43

Browse files
author
Ruchita Deshmukh
committed
1 parent 9f00f52 commit 045da43

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

pandas/core/generic.py

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6670,71 +6670,67 @@ def replace(
66706670
to_replace, value, inplace=inplace, limit=limit, regex=regex
66716671
)
66726672

6673-
66746673
if value is None and not replace_by_none:
66756674

66766675
# passing a single value that is scalar like
66776676
# when value is None (GH5319), for compat
66786677

6679-
if not is_dict_like(to_replace) and not is_dict_like(regex):
6680-
6681-
to_replace = [to_replace]
6682-
6678+
if not is_dict_like(to_replace) and not is_dict_like(regex):
66836679

6684-
if isinstance(to_replace, (tuple, list)):
6680+
to_replace = [to_replace]
66856681

66866682

6687-
if isinstance(self, ABCDataFrame):
6688-
return self.apply(
6689-
_single_replace, args=(to_replace, method, inplace, limit)
6690-
)
6683+
if isinstance(to_replace, (tuple, list)):
6684+
if isinstance(self, ABCDataFrame):
6685+
return self.apply(
6686+
_single_replace, args=(to_replace, method, inplace, limit)
6687+
)
6688+
return _single_replace(self, to_replace, method, inplace, limit)
66916689

6692-
return _single_replace(self, to_replace, method, inplace, limit)
6690+
if not is_dict_like(to_replace):
66936691

6694-
if not is_dict_like(to_replace):
6692+
if not is_dict_like(regex):
6693+
raise TypeError(
6694+
'If "to_replace" and "value" are both None'
6695+
' and "to_replace" is not a list, then '
6696+
"regex must be a mapping"
6697+
)
6698+
to_replace = regex
6699+
regex = True
66956700

6696-
if not is_dict_like(regex):
6697-
raise TypeError(
6698-
'If "to_replace" and "value" are both None'
6699-
' and "to_replace" is not a list, then '
6700-
"regex must be a mapping"
6701-
)
6702-
to_replace = regex
6703-
regex = True
6701+
items = list(to_replace.items())
6702+
keys, values = zip(*items) if items else ([], [])
67046703

6705-
items = list(to_replace.items())
6706-
keys, values = zip(*items) if items else ([], [])
6704+
are_mappings = [is_dict_like(v) for v in values]
67076705

6708-
are_mappings = [is_dict_like(v) for v in values]
6706+
if any(are_mappings):
6707+
if not all(are_mappings):
6708+
raise TypeError(
6709+
"If a nested mapping is passed, all values"
6710+
" of the top level mapping must be "
6711+
"mappings"
6712+
)
6713+
# passed a nested dict/Series
6714+
to_rep_dict = {}
6715+
value_dict = {}
67096716

6710-
if any(are_mappings):
6711-
if not all(are_mappings):
6712-
raise TypeError(
6713-
"If a nested mapping is passed, all values"
6714-
" of the top level mapping must be "
6715-
"mappings"
6717+
for k, v in items:
6718+
keys, values = list(zip(*v.items())) or ([], [])
6719+
if set(keys) & set(values):
6720+
raise ValueError(
6721+
"Replacement not allowed with "
6722+
"overlapping keys and values"
67166723
)
6717-
# passed a nested dict/Series
6718-
to_rep_dict = {}
6719-
value_dict = {}
6720-
6721-
for k, v in items:
6722-
keys, values = list(zip(*v.items())) or ([], [])
6723-
if set(keys) & set(values):
6724-
raise ValueError(
6725-
"Replacement not allowed with "
6726-
"overlapping keys and values"
6727-
)
6728-
to_rep_dict[k] = list(keys)
6729-
value_dict[k] = list(values)
6724+
to_rep_dict[k] = list(keys)
6725+
value_dict[k] = list(values)
67306726

6731-
to_replace, value = to_rep_dict, value_dict
6732-
else:
6733-
to_replace, value = keys, values
6727+
to_replace, value = to_rep_dict, value_dict
6728+
else:
6729+
to_replace, value = keys, values
67346730

6735-
return self.replace(
6736-
to_replace, value, inplace=inplace, limit=limit, regex=regex
6737-
)
6731+
return self.replace(
6732+
to_replace, value, inplace=inplace, limit=limit, regex=regex
6733+
)
67386734
else:
67396735

67406736
# need a non-zero len on all axes

0 commit comments

Comments
 (0)