Skip to content

BUG: GH10395 bug in DataFrame.interpolate with axis=1 and inplace=True #10400

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 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ Bug Fixes
- Bug in ``DataFrame.reset_index`` when index contains `NaT`. (:issue:`10388`)
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)
- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)
41 changes: 22 additions & 19 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,47 +2925,50 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,

if axis == 0:
ax = self._info_axis_name
_maybe_transposed_self = self
elif axis == 1:
self = self.T
_maybe_transposed_self = self.T
ax = 1
ax = self._get_axis_number(ax)
else:
_maybe_transposed_self = self
ax = _maybe_transposed_self._get_axis_number(ax)

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

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

if self._data.get_dtype_counts().get('object') == len(self.T):
if _maybe_transposed_self._data.get_dtype_counts().get('object') == len(_maybe_transposed_self.T):
raise TypeError("Cannot interpolate with all NaNs.")

# create/use the index
if method == 'linear':
index = np.arange(len(self._get_axis(alt_ax))) # prior default
index = np.arange(len(_maybe_transposed_self._get_axis(alt_ax))) # prior default
else:
index = self._get_axis(alt_ax)
index = _maybe_transposed_self._get_axis(alt_ax)

if pd.isnull(index).any():
raise NotImplementedError("Interpolation with NaNs in the index "
"has not been implemented. Try filling "
"those NaNs before interpolating.")
new_data = self._data.interpolate(method=method,
axis=ax,
index=index,
values=self,
limit=limit,
inplace=inplace,
downcast=downcast,
**kwargs)
new_data = _maybe_transposed_self._data.interpolate(
method=method,
axis=ax,
index=index,
values=_maybe_transposed_self,
limit=limit,
inplace=inplace,
downcast=downcast,
**kwargs
)
if inplace:
if axis == 1:
self._update_inplace(new_data)
self = self.T
else:
self._update_inplace(new_data)
new_data = self._constructor(new_data).T._data
self._update_inplace(new_data)
else:
res = self._constructor(new_data).__finalize__(self)
if axis == 1:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,14 @@ def test_interp_inplace(self):
result['a'].interpolate(inplace=True, downcast='infer')
assert_frame_equal(result, expected.astype('int64'))

def test_interp_inplace_row(self):
# GH 10395
result = DataFrame({'a': [1.,2.,3.,4.], 'b': [np.nan, 2., 3., 4.],
'c': [3, 2, 2, 2]})
expected = result.interpolate(method='linear', axis=1, inplace=False)
result.interpolate(method='linear', axis=1, inplace=True)
assert_frame_equal(result, expected)

def test_interp_ignore_all_good(self):
# GH
df = DataFrame({'A': [1, 2, np.nan, 4],
Expand Down