Skip to content

Commit fbd4fcd

Browse files
authored
BUG: merge_asof raising incorrect error for strings (pandas-dev#56444)
1 parent 224ea88 commit fbd4fcd

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ Reshaping
665665
- Bug in :func:`concat` ignoring ``sort`` parameter when passed :class:`DatetimeIndex` indexes (:issue:`54769`)
666666
- Bug in :func:`concat` renaming :class:`Series` when ``ignore_index=False`` (:issue:`15047`)
667667
- Bug in :func:`merge_asof` raising ``TypeError`` when ``by`` dtype is not ``object``, ``int64``, or ``uint64`` (:issue:`22794`)
668+
- Bug in :func:`merge_asof` raising incorrect error for string dtype (:issue:`56444`)
668669
- Bug in :func:`merge` returning columns in incorrect order when left and/or right is empty (:issue:`51929`)
669670
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)
670671
- Bug in :meth:`DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)

pandas/core/reshape/merge.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,12 @@ def _validate_left_right_on(self, left_on, right_on):
19861986
else:
19871987
ro_dtype = self.right.index.dtype
19881988

1989-
if is_object_dtype(lo_dtype) or is_object_dtype(ro_dtype):
1989+
if (
1990+
is_object_dtype(lo_dtype)
1991+
or is_object_dtype(ro_dtype)
1992+
or is_string_dtype(lo_dtype)
1993+
or is_string_dtype(ro_dtype)
1994+
):
19901995
raise MergeError(
19911996
f"Incompatible merge dtype, {repr(ro_dtype)} and "
19921997
f"{repr(lo_dtype)}, both sides must have numeric dtype"

pandas/tests/reshape/merge/test_merge_asof.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Index,
1212
Timedelta,
1313
merge_asof,
14+
option_context,
1415
to_datetime,
1516
)
1617
import pandas._testing as tm
@@ -3372,22 +3373,26 @@ def test_left_index_right_index_tolerance(self, unit):
33723373
tm.assert_frame_equal(result, expected)
33733374

33743375

3376+
@pytest.mark.parametrize(
3377+
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
3378+
)
33753379
@pytest.mark.parametrize(
33763380
"kwargs", [{"on": "x"}, {"left_index": True, "right_index": True}]
33773381
)
33783382
@pytest.mark.parametrize(
33793383
"data",
33803384
[["2019-06-01 00:09:12", "2019-06-01 00:10:29"], [1.0, "2019-06-01 00:10:29"]],
33813385
)
3382-
def test_merge_asof_non_numerical_dtype(kwargs, data):
3386+
def test_merge_asof_non_numerical_dtype(kwargs, data, infer_string):
33833387
# GH#29130
3384-
left = pd.DataFrame({"x": data}, index=data)
3385-
right = pd.DataFrame({"x": data}, index=data)
3386-
with pytest.raises(
3387-
MergeError,
3388-
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
3389-
):
3390-
merge_asof(left, right, **kwargs)
3388+
with option_context("future.infer_string", infer_string):
3389+
left = pd.DataFrame({"x": data}, index=data)
3390+
right = pd.DataFrame({"x": data}, index=data)
3391+
with pytest.raises(
3392+
MergeError,
3393+
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
3394+
):
3395+
merge_asof(left, right, **kwargs)
33913396

33923397

33933398
def test_merge_asof_non_numerical_dtype_object():

0 commit comments

Comments
 (0)