Skip to content

Commit fdf4573

Browse files
committed
Revert "try to fix interpolation problem the elegant way - does not seem to work"
This reverts commit ce22584.
1 parent ce22584 commit fdf4573

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

pandas/core/generic.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -6881,52 +6881,55 @@ def interpolate(
68816881
inplace = validate_bool_kwarg(inplace, "inplace")
68826882

68836883
axis = self._get_axis_number(axis)
6884-
axis_index = self._get_axis(axis)
68856884

6886-
if isinstance(axis_index, MultiIndex) and method != "linear":
6885+
if axis == 0:
6886+
df = self
6887+
else:
6888+
df = self.T
6889+
6890+
if isinstance(df.index, MultiIndex) and method != "linear":
68876891
raise ValueError(
68886892
"Only `method=linear` interpolation is supported on MultiIndexes."
68896893
)
68906894

6891-
if self.ndim == 2 and np.all(self.dtypes == np.dtype(object)):
6895+
if df.ndim == 2 and np.all(df.dtypes == np.dtype(object)):
68926896
raise TypeError(
68936897
"Cannot interpolate with all object-dtype columns "
68946898
"in the DataFrame. Try setting at least one "
68956899
"column to a numeric dtype."
68966900
)
68976901

68986902
# create/use the index
6899-
if method in ["linear"]:
6903+
if method in ["linear", "bfill", "ffill", "pad"]:
69006904
# prior default
6901-
axis_index = np.arange(len(axis_index))
6905+
index = np.arange(len(df.index))
69026906
else:
6903-
methods = {"index", "values", "nearest", "time", "bfill", "ffill", "pad"}
6907+
index = df.index
6908+
methods = {"index", "values", "nearest", "time"}
69046909
is_numeric_or_datetime = (
6905-
is_numeric_dtype(axis_index)
6906-
or is_datetime64_any_dtype(axis_index)
6907-
or is_timedelta64_dtype(axis_index)
6910+
is_numeric_dtype(index)
6911+
or is_datetime64_any_dtype(index)
6912+
or is_timedelta64_dtype(index)
69086913
)
69096914
if method not in methods and not is_numeric_or_datetime:
69106915
raise ValueError(
69116916
"Index column must be numeric or datetime type when "
6912-
f"using {method} method other than linear. "
6917+
f"using {method} method other than linear, bfill, ffill or pad. "
69136918
"Try setting a numeric or datetime index column before "
69146919
"interpolating."
69156920
)
69166921

6917-
if isna(axis_index).any():
6922+
if isna(index).any():
69186923
raise NotImplementedError(
69196924
"Interpolation with NaNs in the index "
69206925
"has not been implemented. Try filling "
69216926
"those NaNs before interpolating."
69226927
)
6923-
data = self._mgr
6924-
print(self)
6925-
print(f'data: {data}')
6928+
data = df._mgr
69266929
new_data = data.interpolate(
69276930
method=method,
6928-
axis=axis,
6929-
index=axis_index,
6931+
axis=self._info_axis_number,
6932+
index=index,
69306933
limit=limit,
69316934
limit_direction=limit_direction,
69326935
limit_area=limit_area,
@@ -6936,6 +6939,8 @@ def interpolate(
69366939
)
69376940

69386941
result = self._constructor(new_data)
6942+
if axis == 1:
6943+
result = result.T
69396944
if inplace:
69406945
return self._update_inplace(result)
69416946
else:

pandas/core/internals/blocks.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,7 @@ def func(x):
12111211
)
12121212

12131213
# interp each column independently
1214-
print(data)
1215-
interp_values = np.apply_along_axis(func, 1, data)
1214+
interp_values = np.apply_along_axis(func, axis, data)
12161215

12171216
blocks = [self.make_block_same_class(interp_values)]
12181217
return self._maybe_downcast(blocks, downcast)

pandas/core/internals/managers.py

-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ def diff(self, n: int, axis: int) -> "BlockManager":
555555
return self.apply("diff", n=n, axis=axis)
556556

557557
def interpolate(self, **kwargs) -> "BlockManager":
558-
#import pdb; pdb.set_trace()
559558
return self.apply("interpolate", **kwargs)
560559

561560
def shift(self, periods: int, axis: int, fill_value) -> "BlockManager":

pandas/tests/frame/methods/test_interpolate.py

+48-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,22 @@ def test_interp_ffill(self, axis):
295295
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
296296
}
297297
)
298-
expected = df.ffill(axis=axis)
298+
if axis == 0:
299+
expected = DataFrame(
300+
{
301+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
302+
"B": [2.0, 4.0, 6.0, 4.0, 8.0, 10.0],
303+
"C": [3.0, 6.0, 9.0, 4.0, 8.0, 30.0],
304+
}
305+
)
306+
if axis == 1:
307+
expected = DataFrame(
308+
{
309+
"A": [1.0, 2.0, 3.0, 4.0, 4.0, 5.0],
310+
"B": [2.0, 4.0, 6.0, 6.0, 8.0, 10.0],
311+
"C": [3.0, 6.0, 9.0, 9.0, 9.0, 30.0],
312+
}
313+
)
299314
result = df.interpolate(method="ffill", axis=axis)
300315
tm.assert_frame_equal(result, expected)
301316

@@ -309,7 +324,22 @@ def test_interp_bfill(self, axis):
309324
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
310325
}
311326
)
312-
expected = df.bfill(axis=axis)
327+
if axis == 0:
328+
expected = DataFrame(
329+
{
330+
"A": [1.0, 2.0, 3.0, 4.0, 8.0, 5.0],
331+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
332+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
333+
}
334+
)
335+
if axis == 1:
336+
expected = DataFrame(
337+
{
338+
"A": [1.0, 2.0, 3.0, 4.0, 5.0, 5.0],
339+
"B": [2.0, 4.0, 6.0, 8.0, 8.0, 10.0],
340+
"C": [3.0, 6.0, 9.0, 30.0, 30.0, 30.0],
341+
}
342+
)
313343
result = df.interpolate(method="bfill", axis=axis)
314344
tm.assert_frame_equal(result, expected)
315345

@@ -323,6 +353,21 @@ def test_interp_pad(self, axis):
323353
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
324354
}
325355
)
326-
expected = df.fillna(method='pad', axis=axis)
356+
if axis == 0:
357+
expected = DataFrame(
358+
{
359+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
360+
"B": [2.0, 4.0, 6.0, 4.0, 8.0, 10.0],
361+
"C": [3.0, 6.0, 9.0, 4.0, 8.0, 30.0],
362+
}
363+
)
364+
if axis == 1:
365+
expected = DataFrame(
366+
{
367+
"A": [1.0, 2.0, 3.0, 4.0, 4.0, 5.0],
368+
"B": [2.0, 4.0, 6.0, 6.0, 8.0, 10.0],
369+
"C": [3.0, 6.0, 9.0, 9.0, 9.0, 30.0],
370+
}
371+
)
327372
result = df.interpolate(method="pad", axis=axis)
328373
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_interpolate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def test_interp_non_timedelta_index(self, interp_methods_ind, ind):
660660
else:
661661
expected_error = (
662662
"Index column must be numeric or datetime type when "
663-
f"using {method} method other than linear. "
663+
f"using {method} method other than linear, bfill, ffill or pad. "
664664
"Try setting a numeric or datetime index column before "
665665
"interpolating."
666666
)

0 commit comments

Comments
 (0)