Skip to content

Commit 012806e

Browse files
committed
fix bfill, ffill and pad when calling with df.interpolate with column dtype string (pandas-dev#33956)
1 parent 0bb677e commit 012806e

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6898,7 +6898,7 @@ def interpolate(
68986898
)
68996899

69006900
# create/use the index
6901-
if method == "linear":
6901+
if method in ["linear", "bfill", "ffill", "pad"]:
69026902
# prior default
69036903
index = np.arange(len(df.index))
69046904
else:
@@ -6912,7 +6912,7 @@ def interpolate(
69126912
if method not in methods and not is_numeric_or_datetime:
69136913
raise ValueError(
69146914
"Index column must be numeric or datetime type when "
6915-
f"using {method} method other than linear. "
6915+
f"using {method} method other than linear, bfill, ffill or pad. "
69166916
"Try setting a numeric or datetime index column before "
69176917
"interpolating."
69186918
)

pandas/tests/frame/methods/test_interpolate.py

+106
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,109 @@ def test_interp_time_inplace_axis(self, axis):
284284
result = expected.interpolate(axis=0, method="time")
285285
expected.interpolate(axis=0, method="time", inplace=True)
286286
tm.assert_frame_equal(result, expected)
287+
288+
def test_interp_ffill(self):
289+
# GH 33956
290+
df = DataFrame(
291+
{
292+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
293+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
294+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
295+
}
296+
)
297+
expected = DataFrame(
298+
{
299+
"A": [1.0, 2.0, 3.0, 4.0, 4.0, 5.0],
300+
"B": [2.0, 4.0, 6.0, 6.0, 8.0, 10.0],
301+
"C": [3.0, 6.0, 9.0, 9.0, 9.0, 30.0],
302+
}
303+
)
304+
result = df.interpolate(method="ffill", axis=1)
305+
tm.assert_frame_equal(result, expected)
306+
307+
@pytest.mark.parametrize("axis", [0, 1])
308+
def test_interp_ffill(self, axis):
309+
# GH 33956
310+
df = DataFrame(
311+
{
312+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
313+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
314+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
315+
}
316+
)
317+
if axis == 0:
318+
expected = DataFrame(
319+
{
320+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
321+
"B": [2.0, 4.0, 6.0, 4.0, 8.0, 10.0],
322+
"C": [3.0, 6.0, 9.0, 4.0, 8.0, 30.0],
323+
}
324+
)
325+
if axis == 1:
326+
expected = DataFrame(
327+
{
328+
"A": [1.0, 2.0, 3.0, 4.0, 4.0, 5.0],
329+
"B": [2.0, 4.0, 6.0, 6.0, 8.0, 10.0],
330+
"C": [3.0, 6.0, 9.0, 9.0, 9.0, 30.0],
331+
}
332+
)
333+
result = df.interpolate(method="ffill", axis=axis)
334+
tm.assert_frame_equal(result, expected)
335+
336+
@pytest.mark.parametrize("axis", [0, 1])
337+
def test_interp_bfill(self, axis):
338+
# GH 33956
339+
df = DataFrame(
340+
{
341+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
342+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
343+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
344+
}
345+
)
346+
if axis == 0:
347+
expected = DataFrame(
348+
{
349+
"A": [1.0, 2.0, 3.0, 4.0, 8.0, 5.0],
350+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
351+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
352+
}
353+
)
354+
if axis == 1:
355+
expected = DataFrame(
356+
{
357+
"A": [1.0, 2.0, 3.0, 4.0, 5.0, 5.0],
358+
"B": [2.0, 4.0, 6.0, 8.0, 8.0, 10.0],
359+
"C": [3.0, 6.0, 9.0, 30.0, 30.0, 30.0],
360+
}
361+
)
362+
result = df.interpolate(method="bfill", axis=axis)
363+
tm.assert_frame_equal(result, expected)
364+
365+
@pytest.mark.parametrize("axis", [0, 1])
366+
def test_interp_pad(self, axis):
367+
# GH 33956
368+
df = DataFrame(
369+
{
370+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
371+
"B": [2.0, 4.0, 6.0, np.nan, 8.0, 10.0],
372+
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
373+
}
374+
)
375+
if axis == 0:
376+
expected = DataFrame(
377+
{
378+
"A": [1.0, 2.0, 3.0, 4.0, np.nan, 5.0],
379+
"B": [2.0, 4.0, 6.0, 4.0, 8.0, 10.0],
380+
"C": [3.0, 6.0, 9.0, 4.0, 8.0, 30.0],
381+
}
382+
)
383+
if axis == 1:
384+
expected = DataFrame(
385+
{
386+
"A": [1.0, 2.0, 3.0, 4.0, 4.0, 5.0],
387+
"B": [2.0, 4.0, 6.0, 6.0, 8.0, 10.0],
388+
"C": [3.0, 6.0, 9.0, 9.0, 9.0, 30.0],
389+
}
390+
)
391+
result = df.interpolate(method="pad", axis=axis)
392+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)