-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
issue #27642 - timedelta merge asof with tolerance #27650
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
TomAugspurger merged 25 commits into pandas-dev:master from ianzur:issue-#27642-timedelta-merge-asof Aug 22, 2019
Merged
Changes from 23 commits
Commits
Show all changes
25 commits Select commit Hold shift + click to select a range
533d407 issue #27642 - timedelta merge asof with tolerance
ianzur 431da66 #27642
ianzur c9acc09 whatsnew
ianzur 76f6525 #27642 added testing stuff
ianzur fd1dbb2 mroeschke: this test is for you :rocket:
ianzur 63b7a2b some words
ianzur 84759ac remove some whitespace
ianzur 04c18e9 a comment
ianzur ac76918 docs change
ianzur 0a0c5c8 Merge remote-tracking branch 'upstream/master' into issue-#27642-time…
ianzur a26bb71 callbacks are :fireworks:
ianzur a0ba5d9 whitespace
ianzur e5456ac little stuff
ianzur 4f56dc4 isort
ianzur 52193a4 I think this'll work
ianzur 7c004c9 cleaner
ianzur 3f8bf2b Merge remote-tracking branch 'upstream/master' into issue-#27642-time…
ianzur 8f8aeb3 :bug: :wrench:
ianzur f66fce4 blacked
ianzur 0eb173f unused import
ianzur 4a1c515 Merge remote-tracking branch 'upstream/master' into issue-#27642-time…
ianzur 6daf823 resolved merge conflict in whatsnew
ianzur 3688986 Merge remote-tracking branch 'upstream/master' into ianzur-issue-#276…
TomAugspurger f6009ca Merge remote-tracking branch 'upstream/master' into ianzur-issue-#276…
TomAugspurger 6f7151d Removed duplicate
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import datetime | ||
| | ||
| import numpy as np | ||
| import pytest | ||
| import pytz | ||
| | @@ -588,14 +590,23 @@ def test_non_sorted(self): | |
| # ok, though has dupes | ||
| merge_asof(trades, self.quotes, on="time", by="ticker") | ||
| | ||
| def test_tolerance(self): | ||
| @pytest.mark.parametrize( | ||
| "tolerance", | ||
| [ | ||
| Timedelta("1day"), | ||
| pytest.param( | ||
| datetime.timedelta(days=1), | ||
| marks=pytest.mark.xfail(reason="not implemented", strict=True), | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ianzur Do we have an open issue to implement this? Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, not that I know of. | ||
| ), | ||
| ], | ||
| ids=["pd.Timedelta", "datetime.timedelta"], | ||
| ) | ||
| def test_tolerance(self, tolerance): | ||
| | ||
| trades = self.trades | ||
| quotes = self.quotes | ||
| | ||
| result = merge_asof( | ||
| trades, quotes, on="time", by="ticker", tolerance=Timedelta("1day") | ||
| ) | ||
| result = merge_asof(trades, quotes, on="time", by="ticker", tolerance=tolerance) | ||
| expected = self.tolerance | ||
| assert_frame_equal(result, expected) | ||
| | ||
| | @@ -1246,3 +1257,39 @@ def test_by_mixed_tz_aware(self): | |
| ) | ||
| expected["value_y"] = np.array([np.nan], dtype=object) | ||
| assert_frame_equal(result, expected) | ||
| | ||
| def test_timedelta_tolerance_nearest(self): | ||
| # GH 27642 | ||
| | ||
| left = pd.DataFrame( | ||
| list(zip([0, 5, 10, 15, 20, 25], [0, 1, 2, 3, 4, 5])), | ||
| columns=["time", "left"], | ||
| ) | ||
| | ||
| left["time"] = pd.to_timedelta(left["time"], "ms") | ||
| | ||
| right = pd.DataFrame( | ||
| list(zip([0, 3, 9, 12, 15, 18], [0, 1, 2, 3, 4, 5])), | ||
| columns=["time", "right"], | ||
| ) | ||
| | ||
| right["time"] = pd.to_timedelta(right["time"], "ms") | ||
| | ||
| expected = pd.DataFrame( | ||
| list( | ||
| zip( | ||
| [0, 5, 10, 15, 20, 25], | ||
| [0, 1, 2, 3, 4, 5], | ||
| [0, np.nan, 2, 4, np.nan, np.nan], | ||
| ) | ||
| ), | ||
| columns=["time", "left", "right"], | ||
| ) | ||
| | ||
| expected["time"] = pd.to_timedelta(expected["time"], "ms") | ||
| | ||
| result = pd.merge_asof( | ||
| left, right, on="time", tolerance=Timedelta("1ms"), direction="nearest" | ||
| ) | ||
| | ||
| assert_frame_equal(result, expected) | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be
if not isinstance(self.tolerance, datetime.timedelta)so users can passdatetime.timedeltaobjects. If you could add a test case for that situation that'd be great.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see commit "mroeschke: this test is for you 🚀"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the test fails
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll need a different PR for this test I think, it fails at this conversion check.
pandas/pandas/core/reshape/merge.py
Lines 1704 to 1710 in 143bc34
With tolerance = datetime.timedelta -> ERROR: has no attribute "value"
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
datetime.timedeltaobjects do not have nanoseconds likepd.TimedeltaAnother check and conversion is necessary to convert
datetime.timedeltacorrectly to nanoseconds similarly topd.Timedelta.valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay thanks for investigating. Yes, I'd be great to address in another issues/PR.