@@ -6670,71 +6670,67 @@ def replace(
6670
6670
to_replace , value , inplace = inplace , limit = limit , regex = regex
6671
6671
)
6672
6672
6673
-
6674
6673
if value is None and not replace_by_none :
6675
6674
6676
6675
# passing a single value that is scalar like
6677
6676
# when value is None (GH5319), for compat
6678
6677
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 ):
6683
6679
6684
- if isinstance ( to_replace , ( tuple , list )):
6680
+ to_replace = [ to_replace ]
6685
6681
6686
6682
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 )
6691
6689
6692
- return _single_replace ( self , to_replace , method , inplace , limit )
6690
+ if not is_dict_like ( to_replace ):
6693
6691
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
6695
6700
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 ([], [])
6704
6703
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 ]
6707
6705
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 = {}
6709
6716
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 "
6716
6723
)
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 )
6730
6726
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
6734
6730
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
+ )
6738
6734
else :
6739
6735
6740
6736
# need a non-zero len on all axes
0 commit comments