Skip to content

Commit 5b48936

Browse files
authored
BUG: merge_asof should be treated as a left join (#34484)
1 parent aa225b2 commit 5b48936

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ Reshaping
787787
^^^^^^^^^
788788
- Bug in :func:`merge` raising error when performing an inner join with partial index and ``right_index`` when no overlap between indices (:issue:`33814`)
789789
- Bug in :meth:`DataFrame.unstack` with missing levels led to incorrect index names (:issue:`37510`)
790+
- Bug in :func:`merge_asof` propagating the right Index with ``left_index=True`` and ``right_on`` specification instead of left Index (:issue:`33463`)
790791
- Bug in :func:`join` over :class:`MultiIndex` returned wrong result, when one of both indexes had only one level (:issue:`36909`)
791792
- :meth:`merge_asof` raises ``ValueError`` instead of cryptic ``TypeError`` in case of non-numerical merge columns (:issue:`29130`)
792793
- Bug in :meth:`DataFrame.join` not assigning values correctly when having :class:`MultiIndex` where at least one dimension is from dtype ``Categorical`` with non-alphabetically sorted categories (:issue:`38502`)

pandas/core/reshape/merge.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,16 @@ def _get_join_info(
969969
join_index = self.right.index.take(right_indexer)
970970
left_indexer = np.array([-1] * len(join_index), dtype=np.intp)
971971
elif self.left_index:
972-
if len(self.right) > 0:
972+
if self.how == "asof":
973+
# GH#33463 asof should always behave like a left merge
974+
join_index = self._create_join_index(
975+
self.left.index,
976+
self.right.index,
977+
left_indexer,
978+
how="left",
979+
)
980+
981+
elif len(self.right) > 0:
973982
join_index = self._create_join_index(
974983
self.right.index,
975984
self.left.index,

pandas/tests/reshape/merge/test_merge_asof.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pandas as pd
88
from pandas import (
9+
Index,
910
Timedelta,
1011
merge_asof,
1112
read_csv,
@@ -1338,7 +1339,9 @@ def test_merge_index_column_tz(self):
13381339
"from_date": index[1:],
13391340
"abc": [2.46] * 3 + [2.19],
13401341
},
1341-
index=pd.Index([1, 2, 3, 4]),
1342+
index=pd.date_range(
1343+
"2019-10-01 00:30:00", freq="30min", periods=4, tz="UTC"
1344+
),
13421345
)
13431346
tm.assert_frame_equal(result, expected)
13441347

@@ -1351,7 +1354,7 @@ def test_merge_index_column_tz(self):
13511354
"abc": [2.46] * 4 + [2.19],
13521355
"xyz": [np.nan, 0.9, 0.8, 0.7, 0.6],
13531356
},
1354-
index=pd.Index([0, 1, 2, 3, 4]),
1357+
index=Index([0, 1, 2, 3, 4]),
13551358
)
13561359
tm.assert_frame_equal(result, expected)
13571360

@@ -1412,3 +1415,25 @@ def test_merge_asof_non_numerical_dtype_object():
14121415
left_by="a",
14131416
right_by="left_val",
14141417
)
1418+
1419+
1420+
@pytest.mark.parametrize(
1421+
"kwargs",
1422+
[
1423+
{"right_index": True, "left_index": True},
1424+
{"left_on": "left_time", "right_index": True},
1425+
{"left_index": True, "right_on": "right"},
1426+
],
1427+
)
1428+
def test_merge_asof_index_behavior(kwargs):
1429+
# GH 33463
1430+
index = Index([1, 5, 10], name="test")
1431+
left = pd.DataFrame({"left": ["a", "b", "c"], "left_time": [1, 4, 10]}, index=index)
1432+
right = pd.DataFrame({"right": [1, 2, 3, 6, 7]}, index=[1, 2, 3, 6, 7])
1433+
result = merge_asof(left, right, **kwargs)
1434+
1435+
expected = pd.DataFrame(
1436+
{"left": ["a", "b", "c"], "left_time": [1, 4, 10], "right": [1, 3, 7]},
1437+
index=index,
1438+
)
1439+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)