Skip to content

Commit 3747e63

Browse files
committed
BUG: GH10395 bug in DataFrame.interpolate with axis=1 and inplace=True
1 parent 8f94f9b commit 3747e63

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ Bug Fixes
7575
- Bug in ``DataFrame.reset_index`` when index contains `NaT`. (:issue:`10388`)
7676
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
7777
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)
78+
- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)

pandas/core/generic.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,47 +2925,50 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
29252925

29262926
if axis == 0:
29272927
ax = self._info_axis_name
2928+
_maybe_transposed_self = self
29282929
elif axis == 1:
2929-
self = self.T
2930+
_maybe_transposed_self = self.T
29302931
ax = 1
2931-
ax = self._get_axis_number(ax)
2932+
else:
2933+
_maybe_transposed_self = self
2934+
ax = _maybe_transposed_self._get_axis_number(ax)
29322935

2933-
if self.ndim == 2:
2936+
if _maybe_transposed_self.ndim == 2:
29342937
alt_ax = 1 - ax
29352938
else:
29362939
alt_ax = ax
29372940

2938-
if isinstance(self.index, MultiIndex) and method != 'linear':
2941+
if isinstance(_maybe_transposed_self.index, MultiIndex) and method != 'linear':
29392942
raise ValueError("Only `method=linear` interpolation is supported "
29402943
"on MultiIndexes.")
29412944

2942-
if self._data.get_dtype_counts().get('object') == len(self.T):
2945+
if _maybe_transposed_self._data.get_dtype_counts().get('object') == len(_maybe_transposed_self.T):
29432946
raise TypeError("Cannot interpolate with all NaNs.")
29442947

29452948
# create/use the index
29462949
if method == 'linear':
2947-
index = np.arange(len(self._get_axis(alt_ax))) # prior default
2950+
index = np.arange(len(_maybe_transposed_self._get_axis(alt_ax))) # prior default
29482951
else:
2949-
index = self._get_axis(alt_ax)
2952+
index = _maybe_transposed_self._get_axis(alt_ax)
29502953

29512954
if pd.isnull(index).any():
29522955
raise NotImplementedError("Interpolation with NaNs in the index "
29532956
"has not been implemented. Try filling "
29542957
"those NaNs before interpolating.")
2955-
new_data = self._data.interpolate(method=method,
2956-
axis=ax,
2957-
index=index,
2958-
values=self,
2959-
limit=limit,
2960-
inplace=inplace,
2961-
downcast=downcast,
2962-
**kwargs)
2958+
new_data = _maybe_transposed_self._data.interpolate(
2959+
method=method,
2960+
axis=ax,
2961+
index=index,
2962+
values=_maybe_transposed_self,
2963+
limit=limit,
2964+
inplace=inplace,
2965+
downcast=downcast,
2966+
**kwargs
2967+
)
29632968
if inplace:
29642969
if axis == 1:
2965-
self._update_inplace(new_data)
2966-
self = self.T
2967-
else:
2968-
self._update_inplace(new_data)
2970+
new_data = self._constructor(new_data).T._data
2971+
self._update_inplace(new_data)
29692972
else:
29702973
res = self._constructor(new_data).__finalize__(self)
29712974
if axis == 1:

pandas/tests/test_generic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,14 @@ def test_interp_inplace(self):
11171117
result['a'].interpolate(inplace=True, downcast='infer')
11181118
assert_frame_equal(result, expected.astype('int64'))
11191119

1120+
def test_interp_inplace_row(self):
1121+
# GH 10395
1122+
result = DataFrame({'a': [1.,2.,3.,4.], 'b': [np.nan, 2., 3., 4.],
1123+
'c': [3, 2, 2, 2]})
1124+
expected = result.interpolate(method='linear', axis=1, inplace=False)
1125+
result.interpolate(method='linear', axis=1, inplace=True)
1126+
assert_frame_equal(result, expected)
1127+
11201128
def test_interp_ignore_all_good(self):
11211129
# GH
11221130
df = DataFrame({'A': [1, 2, np.nan, 4],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy