Skip to content

Commit b58d721

Browse files
committed
better test for pandas-dev#26796
1 parent 16755bd commit b58d721

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

pandas/core/generic.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -6894,7 +6894,9 @@ def replace(
68946894
Update the data in place if possible.
68956895
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
68966896
If limit is specified, consecutive NaNs will be filled in this
6897-
direction.
6897+
direction. If the methods 'pad' or 'ffill' are used it must be
6898+
None or 'forward'. If 'backfill' or 'bfill' are use it must be
6899+
None or 'backwards'.
68986900
limit_area : {`None`, 'inside', 'outside'}, default None
68996901
If limit is specified, consecutive NaNs will be filled with this
69006902
restriction.
@@ -7082,7 +7084,7 @@ def interpolate(
70827084
axis=0,
70837085
limit=None,
70847086
inplace=False,
7085-
limit_direction="forward",
7087+
limit_direction=None,
70867088
limit_area=None,
70877089
max_gap=None,
70887090
downcast=None,
@@ -7123,6 +7125,26 @@ def interpolate(
71237125
"column to a numeric dtype."
71247126
)
71257127

7128+
# Set `limit_direction` depending on `method`
7129+
if (method == 'pad') or (method == 'ffill'):
7130+
if (limit_direction == 'backward') or (limit_direction == 'both'):
7131+
raise ValueError(
7132+
"`limit_direction` must not be `%s` for method `%s`" % (limit_direction, method)
7133+
)
7134+
else:
7135+
limit_direction = 'forward'
7136+
elif (method == 'backfill') or (method == 'bfill'):
7137+
if (limit_direction == 'forward') or (limit_direction == 'both'):
7138+
raise ValueError(
7139+
"`limit_direction` must not be `%s` for method `%s`" % (limit_direction, method)
7140+
)
7141+
else:
7142+
limit_direction = 'backward'
7143+
else:
7144+
# Set default
7145+
if limit_direction is None:
7146+
limit_direction = 'forward'
7147+
71267148
# create/use the index
71277149
if method == "linear":
71287150
# prior default

pandas/tests/series/test_missing.py

+43
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,17 @@ def test_interp_limit_area(self):
13971397
with pytest.raises(ValueError, match=msg):
13981398
s.interpolate(method="linear", limit_area="abc")
13991399

1400+
def test_interp_limit_area_with_pad(self):
1401+
# Test for issue #26796 -- using `limit_area` with `method=pad`
1402+
s = Series([np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan])
1403+
expected = Series([np.nan, np.nan, 3, 3, 3, 3, 7, np.nan, np.nan])
1404+
result = s.interpolate(method="pad", limit_area="inside")
1405+
tm.assert_series_equal(result, expected)
1406+
1407+
expected = Series([np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, 7, 7])
1408+
result = s.interpolate(method="pad", limit_area="outside")
1409+
tm.assert_series_equal(result, expected)
1410+
14001411
def test_interp_limit_direction(self):
14011412
# These tests are for issue #9218 -- fill NaNs in both directions.
14021413
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
@@ -1422,6 +1433,38 @@ def test_interp_limit_direction(self):
14221433
result = s.interpolate(method="linear", limit=1, limit_direction="both")
14231434
tm.assert_series_equal(result, expected)
14241435

1436+
def test_interp_limit_direction_with_pad_error(self):
1437+
# Since `pad` forces a forward fill and `bfill` forces a backward fill
1438+
# they should not be used together with `limit_direction`
1439+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
1440+
1441+
with pytest.raises(
1442+
ValueError, match="`limit_direction` must not be `backward` for method `pad`"
1443+
):
1444+
s.interpolate(method="pad", limit=1, limit_direction="backward")
1445+
1446+
with pytest.raises(
1447+
ValueError, match="`limit_direction` must not be `backward` for method `ffill`"
1448+
):
1449+
s.interpolate(method="ffill", limit=1, limit_direction="backward")
1450+
1451+
with pytest.raises(
1452+
ValueError, match="`limit_direction` must not be `both` for method `ffill`"
1453+
):
1454+
s.interpolate(method="ffill", limit=1, limit_direction="both")
1455+
1456+
with pytest.raises(
1457+
ValueError,
1458+
match="`limit_direction` must not be `forward` for method `backfill`"
1459+
):
1460+
s.interpolate(method="backfill", limit=1, limit_direction="forward")
1461+
1462+
with pytest.raises(
1463+
ValueError,
1464+
match="`limit_direction` must not be `forward` for method `bfill`"
1465+
):
1466+
s.interpolate(method="bfill", limit=1, limit_direction="forward")
1467+
14251468
def test_interp_limit_to_ends(self):
14261469
# These test are for issue #10420 -- flow back to beginning.
14271470
s = Series([np.nan, np.nan, 5, 7, 9, np.nan])

0 commit comments

Comments
 (0)