Skip to content

Commit

Permalink
CLN: de-kludge NDFrame.interpolate (#33084)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Mar 29, 2020
1 parent 6a0acd9 commit e894809
Showing 1 changed file with 13 additions and 26 deletions.
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

0 comments on commit e894809

Please sign in to comment.