Skip to content

BUG: Allow TZ-aware DatetimeIndex in merge_asof() (#14844) #14845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ 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`)
4 changes: 2 additions & 2 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions pandas/tools/tests/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,30 @@ def test_tolerance(self):
expected = self.tolerance
assert_frame_equal(result, expected)

def test_tolerance_tz(self):
# GH 14844
import pytz
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,
Expand Down