Skip to content

Commit 0b1290f

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

File tree

5 files changed

+23
-71
lines changed

5 files changed

+23
-71
lines changed

pandas/core/generic.py

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

68836883
axis = self._get_axis_number(axis)
6884+
axis_index = self._get_axis(axis)
68846885

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

6895-
if df.ndim == 2 and np.all(df.dtypes == np.dtype(object)):
6891+
if self.ndim == 2 and np.all(self.dtypes == np.dtype(object)):
68966892
raise TypeError(
68976893
"Cannot interpolate with all object-dtype columns "
68986894
"in the DataFrame. Try setting at least one "
68996895
"column to a numeric dtype."
69006896
)
69016897

69026898
# create/use the index
6903-
if method in ["linear", "bfill", "ffill", "pad"]:
6899+
if method in ["linear"]:
69046900
# prior default
6905-
index = np.arange(len(df.index))
6901+
axis_index = np.arange(len(axis_index))
69066902
else:
6907-
index = df.index
6908-
methods = {"index", "values", "nearest", "time"}
6903+
methods = {"index", "values", "nearest", "time", "bfill", "ffill", "pad"}
69096904
is_numeric_or_datetime = (
6910-
is_numeric_dtype(index)
6911-
or is_datetime64_any_dtype(index)
6912-
or is_timedelta64_dtype(index)
6905+
is_numeric_dtype(axis_index)
6906+
or is_datetime64_any_dtype(axis_index)
6907+
or is_timedelta64_dtype(axis_index)
69136908
)
69146909
if method not in methods and not is_numeric_or_datetime:
69156910
raise ValueError(
69166911
"Index column must be numeric or datetime type when "
6917-
f"using {method} method other than linear, bfill, ffill or pad. "
6912+
f"using {method} method other than linear. "
69186913
"Try setting a numeric or datetime index column before "
69196914
"interpolating."
69206915
)
69216916

6922-
if isna(index).any():
6917+
if isna(axis_index).any():
69236918
raise NotImplementedError(
69246919
"Interpolation with NaNs in the index "
69256920
"has not been implemented. Try filling "
69266921
"those NaNs before interpolating."
69276922
)
6928-
data = df._mgr
6923+
data = self._mgr
6924+
print(self)
6925+
print(f'data: {data}')
69296926
new_data = data.interpolate(
69306927
method=method,
6931-
axis=self._info_axis_number,
6932-
index=index,
6928+
axis=axis,
6929+
index=axis_index,
69336930
limit=limit,
69346931
limit_direction=limit_direction,
69356932
limit_area=limit_area,
@@ -6939,8 +6936,6 @@ def interpolate(
69396936
)
69406937

69416938
result = self._constructor(new_data)
6942-
if axis == 1:
6943-
result = result.T
69446939
if inplace:
69456940
return self._update_inplace(result)
69466941
else:

pandas/core/internals/blocks.py

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

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

12161217
blocks = [self.make_block_same_class(interp_values)]
12171218
return self._maybe_downcast(blocks, downcast)

pandas/core/internals/managers.py

+1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ 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()
558559
return self.apply("interpolate", **kwargs)
559560

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

pandas/tests/frame/methods/test_interpolate.py

+3-48
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,7 @@ def test_interp_ffill(self, axis):
295295
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
296296
}
297297
)
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-
)
298+
expected = df.ffill(axis=axis)
314299
result = df.interpolate(method="ffill", axis=axis)
315300
tm.assert_frame_equal(result, expected)
316301

@@ -324,22 +309,7 @@ def test_interp_bfill(self, axis):
324309
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
325310
}
326311
)
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-
)
312+
expected = df.bfill(axis=axis)
343313
result = df.interpolate(method="bfill", axis=axis)
344314
tm.assert_frame_equal(result, expected)
345315

@@ -353,21 +323,6 @@ def test_interp_pad(self, axis):
353323
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
354324
}
355325
)
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-
)
326+
expected = df.fillna(method='pad', axis=axis)
372327
result = df.interpolate(method="pad", axis=axis)
373328
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, bfill, ffill or pad. "
663+
f"using {method} method other than linear. "
664664
"Try setting a numeric or datetime index column before "
665665
"interpolating."
666666
)

0 commit comments

Comments
 (0)