Skip to content

Commit e7f668b

Browse files
committed
BUG pandas-devGH-35558 merge_asof error for tolerance with left and right index
1 parent 59ffa25 commit e7f668b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v1.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :meth:`DataFrame.shift` with ``axis=1`` and heterogeneous dtypes (:issue:`35488`)
2323
- Fixed regression in ``.groupby(..).rolling(..)`` where a segfault would occur with ``center=True`` and an odd number of values (:issue:`35552`)
2424
- Fixed regression in :meth:`DataFrame.apply` where functions that altered the input in-place only operated on a single row (:issue:`35462`)
25+
- Fixed regression where :meth:`DataFrame.merge_asof` would raise a ``UnboundLocalError`` when ``left_index`` , ``right_index`` and ``tolerance`` were set (:issue:`35558`)
2526

2627
.. ---------------------------------------------------------------------------
2728

pandas/core/reshape/merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ def _get_merge_keys(self):
16671667

16681668
msg = (
16691669
f"incompatible tolerance {self.tolerance}, must be compat "
1670-
f"with type {repr(lk.dtype)}"
1670+
f"with type {repr(lt.dtype)}"
16711671
)
16721672

16731673
if needs_i8_conversion(lt):

pandas/tests/reshape/merge/test_merge_asof.py

+21
Original file line numberDiff line numberDiff line change
@@ -1339,3 +1339,24 @@ def test_merge_index_column_tz(self):
13391339
index=pd.Index([0, 1, 2, 3, 4]),
13401340
)
13411341
tm.assert_frame_equal(result, expected)
1342+
1343+
def test_left_index_right_index_tolerance(self):
1344+
dr1 = pd.date_range(
1345+
start="1/1/2020", end="1/20/2020", freq="2D"
1346+
) + pd.Timedelta(seconds=0.4)
1347+
dr2 = pd.date_range(start="1/1/2020", end="2/1/2020")
1348+
1349+
df1 = pd.DataFrame({"val1": "foo"}, index=pd.DatetimeIndex(dr1))
1350+
df2 = pd.DataFrame({"val2": "bar"}, index=pd.DatetimeIndex(dr2))
1351+
1352+
expected = pd.DataFrame(
1353+
{"val1": "foo", "val2": "bar"}, index=pd.DatetimeIndex(dr1)
1354+
)
1355+
result = pd.merge_asof(
1356+
df1,
1357+
df2,
1358+
left_index=True,
1359+
right_index=True,
1360+
tolerance=pd.Timedelta(seconds=0.5),
1361+
)
1362+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)