From e991141f3c13117035fb2a08f4e64b4c18c9d5a9 Mon Sep 17 00:00:00 2001 From: "Christopher C. Aycock" Date: Sat, 10 Dec 2016 10:40:17 -0500 Subject: [PATCH] BUG: Allow TZ-aware DatetimeIndex in merge_asof() (#14844) closes #14844 Author: Christopher C. Aycock Closes #14845 from chrisaycock/GH14844 and squashes the following commits: 97b73a8 [Christopher C. Aycock] BUG: Allow TZ-aware DatetimeIndex in merge_asof() (#14844) --- doc/source/whatsnew/v0.19.2.txt | 1 + pandas/tools/merge.py | 4 ++-- pandas/tools/tests/test_merge_asof.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index b4674345dcb96..a9897f389fe12 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -74,6 +74,7 @@ Bug Fixes - Bug in ``pd.read_csv()`` in which the ``nrows`` parameter was not being respected for large input when using the C engine for parsing (:issue:`7626`) +- Bug in ``pd.merge_asof()`` could not handle timezone-aware DatetimeIndex when a tolerance was specified (:issue:`14844`) - Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`) diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index 8d2f92ad58a88..68953c90676dd 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1021,7 +1021,7 @@ def _get_merge_keys(self): msg = "incompatible tolerance, must be compat " \ "with type {0}".format(type(lt)) - if is_datetime64_dtype(lt): + if is_datetime64_dtype(lt) or is_datetime64tz_dtype(lt): if not isinstance(self.tolerance, Timedelta): raise MergeError(msg) if self.tolerance < Timedelta(0): @@ -1034,7 +1034,7 @@ def _get_merge_keys(self): raise MergeError("tolerance must be positive") else: - raise MergeError(msg) + raise MergeError("key must be integer or timestamp") # validate allow_exact_matches if not is_bool(self.allow_exact_matches): diff --git a/pandas/tools/tests/test_merge_asof.py b/pandas/tools/tests/test_merge_asof.py index f413618624592..5c8f424bde7a5 100644 --- a/pandas/tools/tests/test_merge_asof.py +++ b/pandas/tools/tests/test_merge_asof.py @@ -1,6 +1,7 @@ import nose import os +import pytz import numpy as np import pandas as pd from pandas import (merge_asof, read_csv, @@ -293,6 +294,29 @@ def test_tolerance(self): expected = self.tolerance assert_frame_equal(result, expected) + def test_tolerance_tz(self): + # GH 14844 + left = pd.DataFrame( + {'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-02'), + freq='D', periods=5, + tz=pytz.timezone('UTC')), + 'value1': np.arange(5)}) + right = pd.DataFrame( + {'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-01'), + freq='D', periods=5, + tz=pytz.timezone('UTC')), + 'value2': list("ABCDE")}) + result = pd.merge_asof(left, right, on='date', + tolerance=pd.Timedelta('1 day')) + + expected = pd.DataFrame( + {'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-02'), + freq='D', periods=5, + tz=pytz.timezone('UTC')), + 'value1': np.arange(5), + 'value2': list("BCDEE")}) + assert_frame_equal(result, expected) + def test_allow_exact_matches(self): result = merge_asof(self.trades, self.quotes,