@@ -6888,42 +6888,33 @@ def interpolate(
6888
6888
inplace = validate_bool_kwarg (inplace , "inplace" )
6889
6889
6890
6890
axis = self ._get_axis_number (axis )
6891
- index = self ._get_axis (axis )
6892
6891
6893
- if isinstance (self .index , MultiIndex ) and method != "linear" :
6892
+ fillna_methods = ["ffill" , "bfill" , "pad" , "backfill" ]
6893
+ should_transpose = axis == 1 and method not in fillna_methods
6894
+
6895
+ obj = self .T if should_transpose else self
6896
+
6897
+ if method not in fillna_methods :
6898
+ axis = self ._info_axis_number
6899
+
6900
+ if isinstance (obj .index , MultiIndex ) and method != "linear" :
6894
6901
raise ValueError (
6895
6902
"Only `method=linear` interpolation is supported on MultiIndexes."
6896
6903
)
6897
6904
6898
- # for the methods backfill, bfill, pad, ffill limit_direction and limit_area
6899
- # are being ignored, see gh-26796 for more information
6900
- if method in ["backfill" , "bfill" , "pad" , "ffill" ]:
6901
- return self .fillna (
6902
- method = method ,
6903
- axis = axis ,
6904
- inplace = inplace ,
6905
- limit = limit ,
6906
- downcast = downcast ,
6907
- )
6908
-
6909
- # Currently we need this to call the axis correctly inside the various
6910
- # interpolation methods
6911
- if axis == 0 :
6912
- df = self
6913
- else :
6914
- df = self .T
6915
-
6916
- if self .ndim == 2 and np .all (self .dtypes == np .dtype (object )):
6905
+ if obj .ndim == 2 and np .all (obj .dtypes == np .dtype (object )):
6917
6906
raise TypeError (
6918
6907
"Cannot interpolate with all object-dtype columns "
6919
6908
"in the DataFrame. Try setting at least one "
6920
6909
"column to a numeric dtype."
6921
6910
)
6922
6911
6912
+ # create/use the index
6923
6913
if method == "linear" :
6924
6914
# prior default
6925
- index = np .arange (len (df .index ))
6915
+ index = np .arange (len (obj .index ))
6926
6916
else :
6917
+ index = obj .index
6927
6918
methods = {"index" , "values" , "nearest" , "time" }
6928
6919
is_numeric_or_datetime = (
6929
6920
is_numeric_dtype (index .dtype )
@@ -6944,10 +6935,9 @@ def interpolate(
6944
6935
"has not been implemented. Try filling "
6945
6936
"those NaNs before interpolating."
6946
6937
)
6947
- data = df ._mgr
6948
- new_data = data .interpolate (
6938
+ new_data = obj ._mgr .interpolate (
6949
6939
method = method ,
6950
- axis = self . _info_axis_number ,
6940
+ axis = axis ,
6951
6941
index = index ,
6952
6942
limit = limit ,
6953
6943
limit_direction = limit_direction ,
@@ -6958,7 +6948,7 @@ def interpolate(
6958
6948
)
6959
6949
6960
6950
result = self ._constructor (new_data )
6961
- if axis == 1 :
6951
+ if should_transpose :
6962
6952
result = result .T
6963
6953
if inplace :
6964
6954
return self ._update_inplace (result )
0 commit comments