From c4daabb1e5a17910357d028bb54f4cf76530568e Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 27 Aug 2021 20:37:26 +0200 Subject: [PATCH 1/2] Fix regression ignoring arrays in dtype check --- pandas/core/reshape/merge.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 501ad383168a0..88e82f322b39b 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1778,21 +1778,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 From e02792156192e1620705ca4ac0d7cc2496238431 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 19 Nov 2021 22:43:43 +0100 Subject: [PATCH 2/2] Add test --- doc/source/whatsnew/v1.3.5.rst | 1 + pandas/tests/reshape/merge/test_merge_asof.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/doc/source/whatsnew/v1.3.5.rst b/doc/source/whatsnew/v1.3.5.rst index 951b05b65c81b..34e67e51e47e3 100644 --- a/doc/source/whatsnew/v1.3.5.rst +++ b/doc/source/whatsnew/v1.3.5.rst @@ -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`) - diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index 5bb9f56adb8d5..f9310db3123f6 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -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)