Skip to content

CLN: de-kludge NDFrame.interpolate #33084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6835,27 +6835,16 @@ def interpolate(
axis = self._get_axis_number(axis)

if axis == 0:
ax = self._info_axis_name
_maybe_transposed_self = self
elif axis == 1:
_maybe_transposed_self = self.T
ax = 1

ax = _maybe_transposed_self._get_axis_number(ax)

if _maybe_transposed_self.ndim == 2:
alt_ax = 1 - ax
df = self
else:
alt_ax = ax
df = self.T

if isinstance(_maybe_transposed_self.index, MultiIndex) and method != "linear":
if isinstance(df.index, MultiIndex) and method != "linear":
raise ValueError(
"Only `method=linear` interpolation is supported on MultiIndexes."
)

if _maybe_transposed_self._data.get_dtype_counts().get("object") == len(
_maybe_transposed_self.T
):
if df.ndim == 2 and np.all(df.dtypes == np.dtype(object)):
raise TypeError(
"Cannot interpolate with all object-dtype columns "
"in the DataFrame. Try setting at least one "
Expand All @@ -6865,9 +6854,9 @@ def interpolate(
# create/use the index
if method == "linear":
# prior default
index = np.arange(len(_maybe_transposed_self._get_axis(alt_ax)))
index = np.arange(len(df.index))
else:
index = _maybe_transposed_self._get_axis(alt_ax)
index = df.index
methods = {"index", "values", "nearest", "time"}
is_numeric_or_datetime = (
is_numeric_dtype(index)
Expand All @@ -6888,10 +6877,10 @@ def interpolate(
"has not been implemented. Try filling "
"those NaNs before interpolating."
)
data = _maybe_transposed_self._data
data = df._data
new_data = data.interpolate(
method=method,
axis=ax,
axis=self._info_axis_number,
index=index,
limit=limit,
limit_direction=limit_direction,
Expand All @@ -6901,15 +6890,13 @@ def interpolate(
**kwargs,
)

result = self._constructor(new_data)
if axis == 1:
result = result.T
if inplace:
if axis == 1:
new_data = self._constructor(new_data).T._data
self._update_inplace(new_data)
return self._update_inplace(result)
else:
res = self._constructor(new_data).__finalize__(self)
if axis == 1:
res = res.T
return res
return result.__finalize__(self)

# ----------------------------------------------------------------------
# Timeseries methods Methods
Expand Down