Skip to content

Commit b2ffa0b

Browse files
Backport PR #44530: Fix regression ignoring arrays in dtype check for merge_asof (#44538)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 224051d commit b2ffa0b

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

doc/source/whatsnew/v1.3.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in :meth:`Series.equals` when comparing floats with dtype object to None (:issue:`44190`)
18+
- Fixed regression in :func:`merge_asof` raising error when array was supplied as join key (:issue:`42844`)
1819
- Fixed performance regression in :func:`read_csv` (:issue:`44106`)
1920
- Fixed regression in :meth:`Series.duplicated` and :meth:`Series.drop_duplicates` when Series has :class:`Categorical` dtype with boolean categories (:issue:`44351`)
2021
-

pandas/core/reshape/merge.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1781,21 +1781,27 @@ def _validate_specification(self) -> None:
17811781
# GH#29130 Check that merge keys do not have dtype object
17821782
if not self.left_index:
17831783
left_on = self.left_on[0]
1784-
lo_dtype = (
1785-
self.left[left_on].dtype
1786-
if left_on in self.left.columns
1787-
else self.left.index.get_level_values(left_on)
1788-
)
1784+
if is_array_like(left_on):
1785+
lo_dtype = left_on.dtype
1786+
else:
1787+
lo_dtype = (
1788+
self.left[left_on].dtype
1789+
if left_on in self.left.columns
1790+
else self.left.index.get_level_values(left_on)
1791+
)
17891792
else:
17901793
lo_dtype = self.left.index.dtype
17911794

17921795
if not self.right_index:
17931796
right_on = self.right_on[0]
1794-
ro_dtype = (
1795-
self.right[right_on].dtype
1796-
if right_on in self.right.columns
1797-
else self.right.index.get_level_values(right_on)
1798-
)
1797+
if is_array_like(right_on):
1798+
ro_dtype = right_on.dtype
1799+
else:
1800+
ro_dtype = (
1801+
self.right[right_on].dtype
1802+
if right_on in self.right.columns
1803+
else self.right.index.get_level_values(right_on)
1804+
)
17991805
else:
18001806
ro_dtype = self.right.index.dtype
18011807

pandas/tests/reshape/merge/test_merge_asof.py

+41
Original file line numberDiff line numberDiff line change
@@ -1484,3 +1484,44 @@ def test_merge_asof_numeri_column_in_index_object_dtype():
14841484
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
14851485
):
14861486
merge_asof(left, right, left_on="a", right_on="a")
1487+
1488+
1489+
def test_merge_asof_array_as_on():
1490+
# GH#42844
1491+
right = pd.DataFrame(
1492+
{
1493+
"a": [2, 6],
1494+
"ts": [pd.Timestamp("2021/01/01 00:37"), pd.Timestamp("2021/01/01 01:40")],
1495+
}
1496+
)
1497+
ts_merge = pd.date_range(
1498+
start=pd.Timestamp("2021/01/01 00:00"), periods=3, freq="1h"
1499+
)
1500+
left = pd.DataFrame({"b": [4, 8, 7]})
1501+
result = merge_asof(
1502+
left,
1503+
right,
1504+
left_on=ts_merge,
1505+
right_on="ts",
1506+
allow_exact_matches=False,
1507+
direction="backward",
1508+
)
1509+
expected = pd.DataFrame({"b": [4, 8, 7], "a": [np.nan, 2, 6], "ts": ts_merge})
1510+
tm.assert_frame_equal(result, expected)
1511+
1512+
result = merge_asof(
1513+
right,
1514+
left,
1515+
left_on="ts",
1516+
right_on=ts_merge,
1517+
allow_exact_matches=False,
1518+
direction="backward",
1519+
)
1520+
expected = pd.DataFrame(
1521+
{
1522+
"a": [2, 6],
1523+
"ts": [pd.Timestamp("2021/01/01 00:37"), pd.Timestamp("2021/01/01 01:40")],
1524+
"b": [4, 8],
1525+
}
1526+
)
1527+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)