Repository: pandas-dev/pandas
Pull request #55676 (2023-10-25 01:36:19)

Backport PR #55670 on branch 2.1.x (REGR: merge_asof raising TypeError for "by" with datelike dtypes)
Backport PR #55670: REGR: merge_asof raising TypeError for "by" with datelike dtypes


Patch:
diff --git a/doc/source/whatsnew/v2.1.2.rst b/doc/source/whatsnew/v2.1.2.rst
index 8298cc0607..cca508a4d0 100644
--- a/doc/source/whatsnew/v2.1.2.rst
+++ b/doc/source/whatsnew/v2.1.2.rst
@@ -17,6 +17,7 @@ Fixed regressions
 - Fixed regression in :meth:`DataFrame.resample` which was extrapolating back to ``origin`` when ``origin`` was outside its bounds (:issue:`55064`)
 - Fixed regression in :meth:`DataFrame.sort_index` which was not sorting correctly when the index was a sliced :class:`MultiIndex` (:issue:`55379`)
 - Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
+- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
 
 .. ---------------------------------------------------------------------------
 .. _whatsnew_212.bug_fixes:
diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py
index 74181f8dc8..0b343a1fd2 100644
--- a/pandas/core/reshape/merge.py
+++ b/pandas/core/reshape/merge.py
@@ -2193,6 +2193,10 @@ class _AsOfMerge(_OrderedMerge):
                 # TODO: conversions for EAs that can be no-copy.
                 lbv = np.asarray(lbv)
                 rbv = np.asarray(rbv)
+                if needs_i8_conversion(lbv.dtype):
+                    lbv = lbv.view("i8")
+                if needs_i8_conversion(rbv.dtype):
+                    rbv = rbv.view("i8")
             else:
                 # We get here with non-ndarrays in test_merge_by_col_tz_aware
                 #  and test_merge_groupby_multiple_column_with_categorical_column


Test patch:
diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py
index d4cb89dadd..a4ae81f865 100644
--- a/pandas/tests/reshape/merge/test_merge_asof.py
+++ b/pandas/tests/reshape/merge/test_merge_asof.py
@@ -1341,6 +1341,34 @@ class TestAsOfMerge:
         expected["value_y"] = np.array([np.nan], dtype=object)
         tm.assert_frame_equal(result, expected)
 
+    @pytest.mark.parametrize("dtype", ["m8[ns]", "M8[us]"])
+    def test_by_datelike(self, dtype):
+        # GH 55453
+        left = pd.DataFrame(
+            {
+                "by_col": np.array([1], dtype=dtype),
+                "on_col": [2],
+                "value": ["a"],
+            }
+        )
+        right = pd.DataFrame(
+            {
+                "by_col": np.array([1], dtype=dtype),
+                "on_col": [1],
+                "value": ["b"],
+            }
+        )
+        result = merge_asof(left, right, by="by_col", on="on_col")
+        expected = pd.DataFrame(
+            {
+                "by_col": np.array([1], dtype=dtype),
+                "on_col": [2],
+                "value_x": ["a"],
+                "value_y": ["b"],
+            }
+        )
+        tm.assert_frame_equal(result, expected)
+
     def test_timedelta_tolerance_nearest(self, unit):
         # GH 27642
         if unit == "s":