Skip to content
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

BUG: merge_asof raising incorrect error for strings #56444

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/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