From 17c902a18eb3a2e8d725d28a44780b5af92378a0 Mon Sep 17 00:00:00 2001 From: "Christopher C. Aycock" Date: Sun, 15 Jan 2017 20:48:07 -0500 Subject: [PATCH] BUG: Allow tolerance when left_index/right_index enabled for merge_asof() (#15135) --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/tools/merge.py | 6 +++++- pandas/tools/tests/test_merge_asof.py | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 9ea7b740bae8f..1f1b5ea1e38ee 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -358,7 +358,7 @@ Bug Fixes - +- Bug in ``pd.merge_asof()`` where ``left_index``/``right_index`` together caused a failure when ``tolerance`` was specified (:issue:`15135`) diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index 4012629aa3c90..c756a61faeefb 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1129,7 +1129,11 @@ def _get_merge_keys(self): # validate tolerance; must be a Timedelta if we have a DTI if self.tolerance is not None: - lt = left_join_keys[-1] + if self.left_index: + lt = self.left.index + else: + lt = left_join_keys[-1] + msg = "incompatible tolerance, must be compat " \ "with type {0}".format(type(lt)) diff --git a/pandas/tools/tests/test_merge_asof.py b/pandas/tools/tests/test_merge_asof.py index bbbf1a3bdfff9..e05cbd0850169 100644 --- a/pandas/tools/tests/test_merge_asof.py +++ b/pandas/tools/tests/test_merge_asof.py @@ -518,6 +518,19 @@ def test_tolerance_tz(self): 'value2': list("BCDEE")}) assert_frame_equal(result, expected) + def test_index_tolerance(self): + # GH 15135 + expected = self.tolerance.set_index('time') + trades = self.trades.set_index('time') + quotes = self.quotes.set_index('time') + + result = pd.merge_asof(trades, quotes, + left_index=True, + right_index=True, + by='ticker', + tolerance=pd.Timedelta('1day')) + assert_frame_equal(result, expected) + def test_allow_exact_matches(self): result = merge_asof(self.trades, self.quotes,