143
143
arraylike ,
144
144
common ,
145
145
indexing ,
146
+ missing ,
146
147
nanops ,
147
148
sample ,
148
149
)
@@ -7907,20 +7908,7 @@ def interpolate(
7907
7908
"Only `method=linear` interpolation is supported on MultiIndexes."
7908
7909
)
7909
7910
7910
- # Set `limit_direction` depending on `method`
7911
- if limit_direction is None :
7912
- limit_direction = (
7913
- "backward" if method in ("backfill" , "bfill" ) else "forward"
7914
- )
7915
- else :
7916
- if method in ("pad" , "ffill" ) and limit_direction != "forward" :
7917
- raise ValueError (
7918
- f"`limit_direction` must be 'forward' for method `{ method } `"
7919
- )
7920
- if method in ("backfill" , "bfill" ) and limit_direction != "backward" :
7921
- raise ValueError (
7922
- f"`limit_direction` must be 'backward' for method `{ method } `"
7923
- )
7911
+ limit_direction = missing .infer_limit_direction (limit_direction , method )
7924
7912
7925
7913
if obj .ndim == 2 and np .all (obj .dtypes == np .dtype ("object" )):
7926
7914
raise TypeError (
@@ -7929,32 +7917,8 @@ def interpolate(
7929
7917
"column to a numeric dtype."
7930
7918
)
7931
7919
7932
- # create/use the index
7933
- if method == "linear" :
7934
- # prior default
7935
- index = Index (np .arange (len (obj .index )))
7936
- else :
7937
- index = obj .index
7938
- methods = {"index" , "values" , "nearest" , "time" }
7939
- is_numeric_or_datetime = (
7940
- is_numeric_dtype (index .dtype )
7941
- or isinstance (index .dtype , DatetimeTZDtype )
7942
- or lib .is_np_dtype (index .dtype , "mM" )
7943
- )
7944
- if method not in methods and not is_numeric_or_datetime :
7945
- raise ValueError (
7946
- "Index column must be numeric or datetime type when "
7947
- f"using { method } method other than linear. "
7948
- "Try setting a numeric or datetime index column before "
7949
- "interpolating."
7950
- )
7920
+ index = missing .get_interp_index (method , obj .index )
7951
7921
7952
- if isna (index ).any ():
7953
- raise NotImplementedError (
7954
- "Interpolation with NaNs in the index "
7955
- "has not been implemented. Try filling "
7956
- "those NaNs before interpolating."
7957
- )
7958
7922
new_data = obj ._mgr .interpolate (
7959
7923
method = method ,
7960
7924
axis = axis ,
@@ -8140,13 +8104,13 @@ def asof(self, where, subset=None):
8140
8104
locs = self .index .asof_locs (where , ~ (nulls ._values ))
8141
8105
8142
8106
# mask the missing
8143
- missing = locs == - 1
8107
+ mask = locs == - 1
8144
8108
data = self .take (locs )
8145
8109
data .index = where
8146
- if missing .any ():
8110
+ if mask .any ():
8147
8111
# GH#16063 only do this setting when necessary, otherwise
8148
8112
# we'd cast e.g. bools to floats
8149
- data .loc [missing ] = np .nan
8113
+ data .loc [mask ] = np .nan
8150
8114
return data if is_list else data .iloc [- 1 ]
8151
8115
8152
8116
# ----------------------------------------------------------------------
0 commit comments