Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes pandas-dev#55884- Added else for invalid fill_method
Browse files Browse the repository at this point in the history
ziad-kermadi committed Nov 12, 2023
1 parent b2d9ec1 commit f52f278
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
@@ -1863,9 +1863,14 @@ def get_result(self, copy: bool | None = True) -> DataFrame:
right_indexer = cast("npt.NDArray[np.intp]", right_indexer)
left_join_indexer = libjoin.ffill_indexer(left_indexer)
right_join_indexer = libjoin.ffill_indexer(right_indexer)
else:
elif self.fill_method is None:
left_join_indexer = left_indexer
right_join_indexer = right_indexer
else:
raise ValueError(
f"fill_method must be one of ['ffill']. "
f"Got '{self.fill_method}' instead."
)

result = self._reindex_and_concat(
join_index, left_join_indexer, right_join_indexer, copy=copy
23 changes: 23 additions & 0 deletions pandas/tests/reshape/merge/test_merge_ordered.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

@@ -209,3 +211,24 @@ def test_elements_not_in_by_but_in_df(self):
msg = r"\{'h'\} not found in left columns"
with pytest.raises(KeyError, match=msg):
merge_ordered(left, right, on="E", left_by=["G", "h"])

def test_ffill_5584(self):
# GH 5584
df1 = DataFrame({"key": ["a", "c", "e"], "lvalue": [1, 2, 3]})
df2 = DataFrame({"key": ["b", "c", "d", "f"], "rvalue": [4, 5, 6, 7]})

for valid_method in ["ffill", None]:
try:
merge_ordered(df1, df2, on="key", fill_method=valid_method)
except Exception:
pytest.fail(f"Unexpected error with valid fill_method: {valid_method}")

for invalid_method in ["linear", "carrot"]:
with pytest.raises(
ValueError,
match=re.escape(
"fill_method must be one of ['ffill']. "
f"Got '{invalid_method}' instead."
),
):
merge_ordered(df1, df2, on="key", fill_method=invalid_method)

0 comments on commit f52f278

Please sign in to comment.