Skip to content

Commit 0b99114

Browse files
committed
Fixes pandas-dev#55884- Added else for invalid fill_method
1 parent b2d9ec1 commit 0b99114

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pandas/core/reshape/merge.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1863,9 +1863,14 @@ def get_result(self, copy: bool | None = True) -> DataFrame:
18631863
right_indexer = cast("npt.NDArray[np.intp]", right_indexer)
18641864
left_join_indexer = libjoin.ffill_indexer(left_indexer)
18651865
right_join_indexer = libjoin.ffill_indexer(right_indexer)
1866-
else:
1866+
elif self.fill_method is None:
18671867
left_join_indexer = left_indexer
18681868
right_join_indexer = right_indexer
1869+
else:
1870+
raise ValueError(
1871+
f"fill_method must be one of ['ffill']. "
1872+
f"Got '{self.fill_method}' instead."
1873+
)
18691874

18701875
result = self._reindex_and_concat(
18711876
join_index, left_join_indexer, right_join_indexer, copy=copy

pandas/tests/reshape/merge/test_merge_ordered.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -209,3 +211,24 @@ def test_elements_not_in_by_but_in_df(self):
209211
msg = r"\{'h'\} not found in left columns"
210212
with pytest.raises(KeyError, match=msg):
211213
merge_ordered(left, right, on="E", left_by=["G", "h"])
214+
215+
def test_ffill_5584(self):
216+
# GH 5584
217+
df1 = DataFrame({"key": ["a", "c", "e"], "lvalue": [1, 2, 3]})
218+
df2 = DataFrame({"key": ["b", "c", "d", "f"], "rvalue": [4, 5, 6, 7]})
219+
220+
for valid_method in ["ffill", None]:
221+
try:
222+
merge_ordered(df1, df2, on="key", fill_method=valid_method)
223+
except Exception:
224+
pytest.fail(f"Unexpected error with valid fill_method: {valid_method}")
225+
226+
for invalid_method in ["linear", "carrot"]:
227+
with pytest.raises(
228+
ValueError,
229+
match=re.escape(
230+
"fill_method must be one of ['ffill']. "
231+
f"Got '{invalid_method}' instead."
232+
),
233+
):
234+
merge_ordered(df1, df2, on="key", fill_method=invalid_method)

0 commit comments

Comments
 (0)