Skip to content

Commit 4fe91e2

Browse files
Christopher C. Aycockjreback
Christopher C. Aycock
authored andcommitted
BUG: Allow tolerance when left_index/right_index enabled for merge_asof() (pandas-dev#15135)
closes pandas-dev#15135 Author: Christopher C. Aycock <[email protected]> Closes pandas-dev#15139 from chrisaycock/GH15135 and squashes the following commits: 17c902a [Christopher C. Aycock] BUG: Allow tolerance when left_index/right_index enabled for merge_asof() (pandas-dev#15135)
1 parent 0e219d7 commit 4fe91e2

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ Bug Fixes
360360
- Bug in catching an overflow in ``Timestamp`` + ``Timedelta/Offset`` operations (:issue:`15126`)
361361

362362

363-
363+
- Bug in ``pd.merge_asof()`` where ``left_index``/``right_index`` together caused a failure when ``tolerance`` was specified (:issue:`15135`)
364364

365365

366366

pandas/tools/merge.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,11 @@ def _get_merge_keys(self):
11291129
# validate tolerance; must be a Timedelta if we have a DTI
11301130
if self.tolerance is not None:
11311131

1132-
lt = left_join_keys[-1]
1132+
if self.left_index:
1133+
lt = self.left.index
1134+
else:
1135+
lt = left_join_keys[-1]
1136+
11331137
msg = "incompatible tolerance, must be compat " \
11341138
"with type {0}".format(type(lt))
11351139

pandas/tools/tests/test_merge_asof.py

+13
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ def test_tolerance_tz(self):
518518
'value2': list("BCDEE")})
519519
assert_frame_equal(result, expected)
520520

521+
def test_index_tolerance(self):
522+
# GH 15135
523+
expected = self.tolerance.set_index('time')
524+
trades = self.trades.set_index('time')
525+
quotes = self.quotes.set_index('time')
526+
527+
result = pd.merge_asof(trades, quotes,
528+
left_index=True,
529+
right_index=True,
530+
by='ticker',
531+
tolerance=pd.Timedelta('1day'))
532+
assert_frame_equal(result, expected)
533+
521534
def test_allow_exact_matches(self):
522535

523536
result = merge_asof(self.trades, self.quotes,

0 commit comments

Comments
 (0)