Skip to content

Commit 52c9181

Browse files
authored
Fix regression ignoring arrays in dtype check for merge_asof (#44530)
1 parent 0f81a63 commit 52c9181

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
@@ -1783,21 +1783,27 @@ def _validate_specification(self) -> None:
17831783
# GH#29130 Check that merge keys do not have dtype object
17841784
if not self.left_index:
17851785
left_on = self.left_on[0]
1786-
lo_dtype = (
1787-
self.left[left_on].dtype
1788-
if left_on in self.left.columns
1789-
else self.left.index.get_level_values(left_on)
1790-
)
1786+
if is_array_like(left_on):
1787+
lo_dtype = left_on.dtype
1788+
else:
1789+
lo_dtype = (
1790+
self.left[left_on].dtype
1791+
if left_on in self.left.columns
1792+
else self.left.index.get_level_values(left_on)
1793+
)
17911794
else:
17921795
lo_dtype = self.left.index.dtype
17931796

17941797
if not self.right_index:
17951798
right_on = self.right_on[0]
1796-
ro_dtype = (
1797-
self.right[right_on].dtype
1798-
if right_on in self.right.columns
1799-
else self.right.index.get_level_values(right_on)
1800-
)
1799+
if is_array_like(right_on):
1800+
ro_dtype = right_on.dtype
1801+
else:
1802+
ro_dtype = (
1803+
self.right[right_on].dtype
1804+
if right_on in self.right.columns
1805+
else self.right.index.get_level_values(right_on)
1806+
)
18011807
else:
18021808
ro_dtype = self.right.index.dtype
18031809

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)