Skip to content

Commit a27e3f5

Browse files
resolved rebase issue
1 parent cbcc6fa commit a27e3f5

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pandas/core/generic.py

+87
Original file line numberDiff line numberDiff line change
@@ -6962,6 +6962,93 @@ def interpolate(
69626962
3 16.0
69636963
Name: d, dtype: float64
69646964
"""
6965+
inplace = validate_bool_kwarg(inplace, "inplace")
6966+
6967+
axis = self._get_axis_number(axis)
6968+
6969+
fillna_methods = ["ffill", "bfill", "pad", "backfill"]
6970+
should_transpose = axis == 1 and method not in fillna_methods
6971+
6972+
obj = self.T if should_transpose else self
6973+
6974+
if obj.empty:
6975+
return self.copy()
6976+
6977+
if method not in fillna_methods:
6978+
axis = self._info_axis_number
6979+
6980+
if isinstance(obj.index, MultiIndex) and method != "linear":
6981+
raise ValueError(
6982+
"Only `method=linear` interpolation is supported on MultiIndexes."
6983+
)
6984+
6985+
# Set `limit_direction` depending on `method`
6986+
if limit_direction is None:
6987+
limit_direction = (
6988+
"backward" if method in ("backfill", "bfill") else "forward"
6989+
)
6990+
else:
6991+
if method in ("pad", "ffill") and limit_direction != "forward":
6992+
raise ValueError(
6993+
f"`limit_direction` must be 'forward' for method `{method}`"
6994+
)
6995+
if method in ("backfill", "bfill") and limit_direction != "backward":
6996+
raise ValueError(
6997+
f"`limit_direction` must be 'backward' for method `{method}`"
6998+
)
6999+
7000+
if obj.ndim == 2 and np.all(obj.dtypes == np.dtype(object)):
7001+
raise TypeError(
7002+
"Cannot interpolate with all object-dtype columns "
7003+
"in the DataFrame. Try setting at least one "
7004+
"column to a numeric dtype."
7005+
)
7006+
7007+
# create/use the index
7008+
if method == "linear":
7009+
# prior default
7010+
index = np.arange(len(obj.index))
7011+
else:
7012+
index = obj.index
7013+
methods = {"index", "values", "nearest", "time"}
7014+
is_numeric_or_datetime = (
7015+
is_numeric_dtype(index.dtype)
7016+
or is_datetime64_any_dtype(index.dtype)
7017+
or is_timedelta64_dtype(index.dtype)
7018+
)
7019+
if method not in methods and not is_numeric_or_datetime:
7020+
raise ValueError(
7021+
"Index column must be numeric or datetime type when "
7022+
f"using {method} method other than linear. "
7023+
"Try setting a numeric or datetime index column before "
7024+
"interpolating."
7025+
)
7026+
7027+
if isna(index).any():
7028+
raise NotImplementedError(
7029+
"Interpolation with NaNs in the index "
7030+
"has not been implemented. Try filling "
7031+
"those NaNs before interpolating."
7032+
)
7033+
new_data = obj._mgr.interpolate(
7034+
method=method,
7035+
axis=axis,
7036+
index=index,
7037+
limit=limit,
7038+
limit_direction=limit_direction,
7039+
limit_area=limit_area,
7040+
inplace=inplace,
7041+
downcast=downcast,
7042+
**kwargs,
7043+
)
7044+
7045+
result = self._constructor(new_data)
7046+
if should_transpose:
7047+
result = result.T
7048+
if inplace:
7049+
return self._update_inplace(result)
7050+
else:
7051+
return result.__finalize__(self, method="interpolate")
69657052

69667053
# ----------------------------------------------------------------------
69677054
# Timeseries methods Methods

0 commit comments

Comments
 (0)