Skip to content

Datetime mergeasof tolerance #28241

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

Merged
merged 15 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Reshaping
- Bug in :meth:`DataFrame.crosstab` when ``margins`` set to ``True`` and ``normalize`` is not ``False``, an error is raised. (:issue:`27500`)
- :meth:`DataFrame.join` now suppresses the ``FutureWarning`` when the sort parameter is specified (:issue:`21952`)
- Bug in :meth:`DataFrame.join` raising with readonly arrays (:issue:`27943`)
- Bug :meth:`merge_asof` could not use datetime.timedelta for `tolerance` kwarg (:issue:`28098`)

Sparse
^^^^^^
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import string
import warnings

import datetime
import numpy as np

from pandas._libs import hashtable as libhashtable, lib
Expand Down Expand Up @@ -1619,7 +1620,7 @@ def _get_merge_keys(self):
)
raise MergeError(msg)

# validate tolerance; must be a Timedelta if we have a DTI
# validate tolerance; datetime.timedelta or Timedelta if we have a DTI
if self.tolerance is not None:

if self.left_index:
Expand All @@ -1635,7 +1636,7 @@ def _get_merge_keys(self):
)

if is_datetimelike(lt):
if not isinstance(self.tolerance, Timedelta):
if not isinstance(self.tolerance, datetime.timedelta):
raise MergeError(msg)
if self.tolerance < Timedelta(0):
raise MergeError("tolerance must be positive")
Expand Down Expand Up @@ -1705,7 +1706,11 @@ def flip(xs):
left_values = left_values.view("i8")
right_values = right_values.view("i8")
if tolerance is not None:
tolerance = tolerance.value
try:
tolerance = tolerance.value
except AttributeError:
# datetime.timedelta(microseconds=1) == min resolution, cvt to nano
tolerance = (tolerance / datetime.timedelta(microseconds=1)) * 1000

# a "by" parameter requires special handling
if self.left_by is not None:
Expand Down
8 changes: 1 addition & 7 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,7 @@ def test_non_sorted(self):

@pytest.mark.parametrize(
"tolerance",
[
Timedelta("1day"),
pytest.param(
datetime.timedelta(days=1),
marks=pytest.mark.xfail(reason="not implemented", strict=True),
),
],
[Timedelta("1day"), datetime.timedelta(days=1)],
ids=["pd.Timedelta", "datetime.timedelta"],
)
def test_tolerance(self, tolerance):
Expand Down