Skip to content

Commit 4976ee1

Browse files
svenharrismroeschke
authored andcommitted
Merge asof float fix (pandas-dev#22982)
* allow tolerance in merge_asof with float type * BUG: GH22981 allow tolerance in merge_asof with float type * BUG: GH22981 allow tolerance in merge_asof with float type
1 parent a6c1ff1 commit 4976ee1

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ Reshaping
849849
- Bug in :meth:`DataFrame.drop_duplicates` for empty ``DataFrame`` which incorrectly raises an error (:issue:`20516`)
850850
- 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`)
851851
- Bug in :func:`merge` when merging ``datetime64[ns, tz]`` data that contained a DST transition (:issue:`18885`)
852+
- Bug in :func:`merge_asof` when merging on float values within defined tolerance (:issue:`22981`)
852853

853854
Build Changes
854855
^^^^^^^^^^^^^

pandas/core/reshape/merge.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
is_categorical_dtype,
2424
is_integer_dtype,
2525
is_float_dtype,
26+
is_number,
2627
is_numeric_dtype,
2728
is_integer,
2829
is_int_or_datetime_dtype,
@@ -1356,8 +1357,14 @@ def _get_merge_keys(self):
13561357
if self.tolerance < 0:
13571358
raise MergeError("tolerance must be positive")
13581359

1360+
elif is_float_dtype(lt):
1361+
if not is_number(self.tolerance):
1362+
raise MergeError(msg)
1363+
if self.tolerance < 0:
1364+
raise MergeError("tolerance must be positive")
1365+
13591366
else:
1360-
raise MergeError("key must be integer or timestamp")
1367+
raise MergeError("key must be integer, timestamp or float")
13611368

13621369
# validate allow_exact_matches
13631370
if not is_bool(self.allow_exact_matches):

pandas/tests/reshape/merge/test_merge_asof.py

+15
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,21 @@ def test_tolerance_tz(self):
642642
'value2': list("BCDEE")})
643643
assert_frame_equal(result, expected)
644644

645+
def test_tolerance_float(self):
646+
# GH22981
647+
left = pd.DataFrame({'a': [1.1, 3.5, 10.9],
648+
'left_val': ['a', 'b', 'c']})
649+
right = pd.DataFrame({'a': [1.0, 2.5, 3.3, 7.5, 11.5],
650+
'right_val': [1.0, 2.5, 3.3, 7.5, 11.5]})
651+
652+
expected = pd.DataFrame({'a': [1.1, 3.5, 10.9],
653+
'left_val': ['a', 'b', 'c'],
654+
'right_val': [1, 3.3, np.nan]})
655+
656+
result = pd.merge_asof(left, right, on='a', direction='nearest',
657+
tolerance=0.5)
658+
assert_frame_equal(result, expected)
659+
645660
def test_index_tolerance(self):
646661
# GH 15135
647662
expected = self.tolerance.set_index('time')

0 commit comments

Comments
 (0)