Skip to content

Commit 6241b9d

Browse files
jjlkantjreback
authored andcommitted
BUG: Allow all int types for merge (GH28870) (#28875)
1 parent 3954fa7 commit 6241b9d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ Reshaping
345345
- Bug :func:`merge_asof` could not use :class:`datetime.timedelta` for ``tolerance`` kwarg (:issue:`28098`)
346346
- Bug in :func:`merge`, did not append suffixes correctly with MultiIndex (:issue:`28518`)
347347
- :func:`qcut` and :func:`cut` now handle boolean input (:issue:`20303`)
348+
- Fix to ensure all int dtypes can be used in :func:`merge_asof` when using a tolerance value. Previously every non-int64 type would raise an erroneous ``MergeError`` (:issue:`28870`).
348349

349350
Sparse
350351
^^^^^^

pandas/core/reshape/merge.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
is_dtype_equal,
2929
is_extension_array_dtype,
3030
is_float_dtype,
31-
is_int64_dtype,
3231
is_integer,
3332
is_integer_dtype,
3433
is_list_like,
@@ -1641,7 +1640,7 @@ def _get_merge_keys(self):
16411640
if self.tolerance < Timedelta(0):
16421641
raise MergeError("tolerance must be positive")
16431642

1644-
elif is_int64_dtype(lt):
1643+
elif is_integer_dtype(lt):
16451644
if not is_integer(self.tolerance):
16461645
raise MergeError(msg)
16471646
if self.tolerance < 0:

pandas/tests/reshape/merge/test_merge_asof.py

+16
Original file line numberDiff line numberDiff line change
@@ -1287,3 +1287,19 @@ def test_timedelta_tolerance_nearest(self):
12871287
)
12881288

12891289
assert_frame_equal(result, expected)
1290+
1291+
def test_int_type_tolerance(self, any_int_dtype):
1292+
# GH #28870
1293+
1294+
left = pd.DataFrame({"a": [0, 10, 20], "left_val": [1, 2, 3]})
1295+
right = pd.DataFrame({"a": [5, 15, 25], "right_val": [1, 2, 3]})
1296+
left["a"] = left["a"].astype(any_int_dtype)
1297+
right["a"] = right["a"].astype(any_int_dtype)
1298+
1299+
expected = pd.DataFrame(
1300+
{"a": [0, 10, 20], "left_val": [1, 2, 3], "right_val": [np.nan, 1.0, 2.0]}
1301+
)
1302+
expected["a"] = expected["a"].astype(any_int_dtype)
1303+
1304+
result = pd.merge_asof(left, right, on="a", tolerance=10)
1305+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)