Skip to content

Commit

Permalink
Backport PR pandas-dev#57018: REGR: merge_ordered with fill_method="f…
Browse files Browse the repository at this point in the history
…fill" and how="left"
  • Loading branch information
lukemanley authored and meeseeksmachine committed Jan 22, 2024
1 parent 987dcbb commit 845c01e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)

.. ---------------------------------------------------------------------------
.. _whatsnew_221.bug_fixes:
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1930,10 +1930,9 @@ def get_result(self, copy: bool | None = True) -> DataFrame:

if self.fill_method == "ffill":
if left_indexer is None:
raise TypeError("left_indexer cannot be None")
left_indexer = cast("npt.NDArray[np.intp]", left_indexer)
right_indexer = cast("npt.NDArray[np.intp]", right_indexer)
left_join_indexer = libjoin.ffill_indexer(left_indexer)
left_join_indexer = None
else:
left_join_indexer = libjoin.ffill_indexer(left_indexer)
if right_indexer is None:
right_join_indexer = None
else:
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/reshape/merge/test_merge_ordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,26 @@ def test_ffill_validate_fill_method(self, left, right, invalid_method):
ValueError, match=re.escape("fill_method must be 'ffill' or None")
):
merge_ordered(left, right, on="key", fill_method=invalid_method)

def test_ffill_left_merge(self):
# GH 57010
df1 = DataFrame(
{
"key": ["a", "c", "e", "a", "c", "e"],
"lvalue": [1, 2, 3, 1, 2, 3],
"group": ["a", "a", "a", "b", "b", "b"],
}
)
df2 = DataFrame({"key": ["b", "c", "d"], "rvalue": [1, 2, 3]})
result = merge_ordered(
df1, df2, fill_method="ffill", left_by="group", how="left"
)
expected = DataFrame(
{
"key": ["a", "c", "e", "a", "c", "e"],
"lvalue": [1, 2, 3, 1, 2, 3],
"group": ["a", "a", "a", "b", "b", "b"],
"rvalue": [np.nan, 2.0, 2.0, np.nan, 2.0, 2.0],
}
)
tm.assert_frame_equal(result, expected)

0 comments on commit 845c01e

Please sign in to comment.