Skip to content

Commit

Permalink
BUG: merge_asof raising incorrect error for strings (#56444)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Dec 11, 2023
1 parent 224ea88 commit fbd4fcd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ Reshaping
- Bug in :func:`concat` ignoring ``sort`` parameter when passed :class:`DatetimeIndex` indexes (:issue:`54769`)
- Bug in :func:`concat` renaming :class:`Series` when ``ignore_index=False`` (:issue:`15047`)
- Bug in :func:`merge_asof` raising ``TypeError`` when ``by`` dtype is not ``object``, ``int64``, or ``uint64`` (:issue:`22794`)
- Bug in :func:`merge_asof` raising incorrect error for string dtype (:issue:`56444`)
- Bug in :func:`merge` returning columns in incorrect order when left and/or right is empty (:issue:`51929`)
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)
- Bug in :meth:`DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,12 @@ def _validate_left_right_on(self, left_on, right_on):
else:
ro_dtype = self.right.index.dtype

if is_object_dtype(lo_dtype) or is_object_dtype(ro_dtype):
if (
is_object_dtype(lo_dtype)
or is_object_dtype(ro_dtype)
or is_string_dtype(lo_dtype)
or is_string_dtype(ro_dtype)
):
raise MergeError(
f"Incompatible merge dtype, {repr(ro_dtype)} and "
f"{repr(lo_dtype)}, both sides must have numeric dtype"
Expand Down
21 changes: 13 additions & 8 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Index,
Timedelta,
merge_asof,
option_context,
to_datetime,
)
import pandas._testing as tm
Expand Down Expand Up @@ -3372,22 +3373,26 @@ def test_left_index_right_index_tolerance(self, unit):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
)
@pytest.mark.parametrize(
"kwargs", [{"on": "x"}, {"left_index": True, "right_index": True}]
)
@pytest.mark.parametrize(
"data",
[["2019-06-01 00:09:12", "2019-06-01 00:10:29"], [1.0, "2019-06-01 00:10:29"]],
)
def test_merge_asof_non_numerical_dtype(kwargs, data):
def test_merge_asof_non_numerical_dtype(kwargs, data, infer_string):
# GH#29130
left = pd.DataFrame({"x": data}, index=data)
right = pd.DataFrame({"x": data}, index=data)
with pytest.raises(
MergeError,
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
):
merge_asof(left, right, **kwargs)
with option_context("future.infer_string", infer_string):
left = pd.DataFrame({"x": data}, index=data)
right = pd.DataFrame({"x": data}, index=data)
with pytest.raises(
MergeError,
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
):
merge_asof(left, right, **kwargs)


def test_merge_asof_non_numerical_dtype_object():
Expand Down

0 comments on commit fbd4fcd

Please sign in to comment.