@@ -2649,44 +2649,14 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
2649
2649
self ._consolidate_inplace ()
2650
2650
2651
2651
if value is None :
2652
- if self ._is_mixed_type and axis == 1 :
2653
- return self .T .replace (to_replace , method = method , limit = limit ).T
2654
-
2655
- method = com ._clean_fill_method (method )
2656
-
2657
- if isinstance (to_replace , dict ):
2658
- if axis == 1 :
2659
- return self .T .replace (to_replace , method = method ,
2660
- limit = limit ).T
2661
-
2662
- rs = self if inplace else self .copy ()
2663
- for k , v in to_replace .iteritems ():
2664
- if k in rs :
2665
- rs [k ].replace (v , method = method , limit = limit ,
2666
- inplace = True )
2667
- return rs
2668
-
2669
- else :
2670
- new_blocks = []
2671
- for block in self ._data .blocks :
2672
- newb = block .interpolate (method , axis = axis ,
2673
- limit = limit , inplace = inplace ,
2674
- missing = to_replace )
2675
- new_blocks .append (newb )
2676
- new_data = BlockManager (new_blocks , self ._data .axes )
2677
-
2678
- if inplace :
2679
- self ._data = new_data
2680
- return self
2681
- else :
2682
- return self ._constructor (new_data )
2683
-
2652
+ return self ._interpolate (to_replace , method , axis , inplace , limit )
2684
2653
else :
2685
2654
# Float type values
2686
2655
if len (self .columns ) == 0 :
2687
2656
return self
2688
2657
2689
2658
if np .isscalar (to_replace ):
2659
+
2690
2660
if np .isscalar (value ): # np.nan -> 0
2691
2661
new_data = self ._data .replace (to_replace , value ,
2692
2662
inplace = inplace )
@@ -2699,14 +2669,17 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
2699
2669
elif isinstance (value , dict ): # np.nan -> {'A' : 0, 'B' : -1}
2700
2670
return self ._replace_dest_dict (to_replace , value , inplace )
2701
2671
2672
+
2702
2673
elif isinstance (to_replace , dict ):
2674
+
2703
2675
if np .isscalar (value ): # {'A' : np.nan, 'B' : ''} -> 0
2704
2676
return self ._replace_src_dict (to_replace , value , inplace )
2677
+
2705
2678
elif isinstance (value , dict ): # {'A' : np.nan} -> {'A' : 0}
2706
2679
return self ._replace_both_dict (to_replace , value , inplace )
2707
- else :
2708
- raise ValueError ('Fill value must be scalar or dict' )
2709
- return rs
2680
+
2681
+ raise ValueError ('Fill value must be scalar or dict' )
2682
+
2710
2683
2711
2684
elif isinstance (to_replace , (list , np .ndarray )):
2712
2685
# [np.nan, ''] -> [0, 'missing']
@@ -2723,14 +2696,48 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
2723
2696
else : # [np.nan, ''] -> 0
2724
2697
new_data = self ._data .replace (to_replace , value ,
2725
2698
inplace = inplace )
2699
+
2726
2700
if inplace :
2727
2701
self ._data = new_data
2728
2702
return self
2729
2703
else :
2730
2704
return self ._constructor (new_data )
2705
+
2706
+ raise ValueError ('Invalid to_replace type: %s' % type (to_replace ))
2707
+
2708
+ def _interpolate (self , to_replace , method , axis , inplace , limit ):
2709
+ if self ._is_mixed_type and axis == 1 :
2710
+ return self .T .replace (to_replace , method = method , limit = limit ).T
2711
+
2712
+ method = com ._clean_fill_method (method )
2713
+
2714
+ if isinstance (to_replace , dict ):
2715
+ if axis == 1 :
2716
+ return self .T .replace (to_replace , method = method ,
2717
+ limit = limit ).T
2718
+
2719
+ rs = self if inplace else self .copy ()
2720
+ for k , v in to_replace .iteritems ():
2721
+ if k in rs :
2722
+ rs [k ].replace (v , method = method , limit = limit ,
2723
+ inplace = True )
2724
+ return rs
2725
+
2726
+ else :
2727
+ new_blocks = []
2728
+ for block in self ._data .blocks :
2729
+ newb = block .interpolate (method , axis = axis ,
2730
+ limit = limit , inplace = inplace ,
2731
+ missing = to_replace )
2732
+ new_blocks .append (newb )
2733
+ new_data = BlockManager (new_blocks , self ._data .axes )
2734
+
2735
+ if inplace :
2736
+ self ._data = new_data
2737
+ return self
2731
2738
else :
2732
- raise ValueError ( 'Invalid to_replace type: %s' %
2733
- type ( to_replace ))
2739
+ return self . _constructor ( new_data )
2740
+
2734
2741
2735
2742
def _replace_dest_dict (self , to_replace , value , inplace ):
2736
2743
rs = self if inplace else self .copy ()
0 commit comments