Skip to content

Fix regression ignoring arrays in dtype check for merge_asof #44530

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

Merged
merged 3 commits into from
Nov 20, 2021
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/v1.3.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`Series.equals` when comparing floats with dtype object to None (:issue:`44190`)
- Fixed regression in :func:`merge_asof` raising error when array was supplied as join key (:issue:`42844`)
- Fixed performance regression in :func:`read_csv` (:issue:`44106`)
- Fixed regression in :meth:`Series.duplicated` and :meth:`Series.drop_duplicates` when Series has :class:`Categorical` dtype with boolean categories (:issue:`44351`)
-
Expand Down
26 changes: 16 additions & 10 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,21 +1783,27 @@ def _validate_specification(self) -> None:
# GH#29130 Check that merge keys do not have dtype object
if not self.left_index:
left_on = self.left_on[0]
lo_dtype = (
self.left[left_on].dtype
if left_on in self.left.columns
else self.left.index.get_level_values(left_on)
)
if is_array_like(left_on):
lo_dtype = left_on.dtype
else:
lo_dtype = (
self.left[left_on].dtype
if left_on in self.left.columns
else self.left.index.get_level_values(left_on)
)
else:
lo_dtype = self.left.index.dtype

if not self.right_index:
right_on = self.right_on[0]
ro_dtype = (
self.right[right_on].dtype
if right_on in self.right.columns
else self.right.index.get_level_values(right_on)
)
if is_array_like(right_on):
ro_dtype = right_on.dtype
else:
ro_dtype = (
self.right[right_on].dtype
if right_on in self.right.columns
else self.right.index.get_level_values(right_on)
)
else:
ro_dtype = self.right.index.dtype

Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,3 +1484,44 @@ def test_merge_asof_numeri_column_in_index_object_dtype():
match=r"Incompatible merge dtype, .*, both sides must have numeric dtype",
):
merge_asof(left, right, left_on="a", right_on="a")


def test_merge_asof_array_as_on():
# GH#42844
right = pd.DataFrame(
{
"a": [2, 6],
"ts": [pd.Timestamp("2021/01/01 00:37"), pd.Timestamp("2021/01/01 01:40")],
}
)
ts_merge = pd.date_range(
start=pd.Timestamp("2021/01/01 00:00"), periods=3, freq="1h"
)
left = pd.DataFrame({"b": [4, 8, 7]})
result = merge_asof(
left,
right,
left_on=ts_merge,
right_on="ts",
allow_exact_matches=False,
direction="backward",
)
expected = pd.DataFrame({"b": [4, 8, 7], "a": [np.nan, 2, 6], "ts": ts_merge})
tm.assert_frame_equal(result, expected)

result = merge_asof(
right,
left,
left_on="ts",
right_on=ts_merge,
allow_exact_matches=False,
direction="backward",
)
expected = pd.DataFrame(
{
"a": [2, 6],
"ts": [pd.Timestamp("2021/01/01 00:37"), pd.Timestamp("2021/01/01 01:40")],
"b": [4, 8],
}
)
tm.assert_frame_equal(result, expected)