Skip to content

Merge asof float fix #22982

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 4 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ Reshaping
- Bug in :meth:`DataFrame.drop_duplicates` for empty ``DataFrame`` which incorrectly raises an error (:issue:`20516`)
- Bug in :func:`pandas.wide_to_long` when a string is passed to the stubnames argument and a column name is a substring of that stubname (:issue:`22468`)
- Bug in :func:`merge` when merging ``datetime64[ns, tz]`` data that contained a DST transition (:issue:`18885`)
- Bug in :func: `merge_asof` when merging on float values within defined tolerance (:issue:`22981`)

Build Changes
^^^^^^^^^^^^^
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
is_categorical_dtype,
is_integer_dtype,
is_float_dtype,
is_number,
is_numeric_dtype,
is_integer,
is_int_or_datetime_dtype,
Expand Down Expand Up @@ -1356,8 +1357,14 @@ def _get_merge_keys(self):
if self.tolerance < 0:
raise MergeError("tolerance must be positive")

elif is_float_dtype(lt):
if not is_number(self.tolerance):
raise MergeError(msg)
if self.tolerance < 0:
raise MergeError("tolerance must be positive")

else:
raise MergeError("key must be integer or timestamp")
raise MergeError("key must be integer, timestamp or float")

# validate allow_exact_matches
if not is_bool(self.allow_exact_matches):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ def test_tolerance_tz(self):
'value2': list("BCDEE")})
assert_frame_equal(result, expected)

def test_tolerance_float(self):
left = pd.DataFrame({'a': [1.1, 3.5, 10.9],
'left_val': ['a', 'b', 'c']})
right = pd.DataFrame({'a': [1.0, 2.5, 3.3, 7.5, 11.5],
'right_val': [1.0, 2.5, 3.3, 7.5, 11.5]})

expected = pd.DataFrame({'a': [1.1, 3.5, 10.9],
'left_val': ['a', 'b', 'c'],
'right_val': [1, 3.3, np.nan]})

result = pd.merge_asof(left, right, on='a', direction='nearest',
tolerance=0.5)
assert_frame_equal(result, expected)

def test_index_tolerance(self):
# GH 15135
expected = self.tolerance.set_index('time')
Expand Down