Skip to content

Commit 8b5ba3c

Browse files
authored
REGR: merge_asof raising TypeError for "by" with datelike dtypes (#55670)
fix regression in merge_asof by datelike
1 parent 0cd5428 commit 8b5ba3c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Fixed regression in :meth:`DataFrame.resample` which was extrapolating back to ``origin`` when ``origin`` was outside its bounds (:issue:`55064`)
1818
- Fixed regression in :meth:`DataFrame.sort_index` which was not sorting correctly when the index was a sliced :class:`MultiIndex` (:issue:`55379`)
1919
- Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
20+
- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
2021

2122
.. ---------------------------------------------------------------------------
2223
.. _whatsnew_212.bug_fixes:

pandas/core/reshape/merge.py

+4
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,10 @@ def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]
21672167
# TODO: conversions for EAs that can be no-copy.
21682168
lbv = np.asarray(lbv)
21692169
rbv = np.asarray(rbv)
2170+
if needs_i8_conversion(lbv.dtype):
2171+
lbv = lbv.view("i8")
2172+
if needs_i8_conversion(rbv.dtype):
2173+
rbv = rbv.view("i8")
21702174
else:
21712175
# We get here with non-ndarrays in test_merge_by_col_tz_aware
21722176
# and test_merge_groupby_multiple_column_with_categorical_column

pandas/tests/reshape/merge/test_merge_asof.py

+28
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,34 @@ def test_by_mixed_tz_aware(self):
13451345
expected["value_y"] = np.array([np.nan], dtype=object)
13461346
tm.assert_frame_equal(result, expected)
13471347

1348+
@pytest.mark.parametrize("dtype", ["m8[ns]", "M8[us]"])
1349+
def test_by_datelike(self, dtype):
1350+
# GH 55453
1351+
left = pd.DataFrame(
1352+
{
1353+
"by_col": np.array([1], dtype=dtype),
1354+
"on_col": [2],
1355+
"value": ["a"],
1356+
}
1357+
)
1358+
right = pd.DataFrame(
1359+
{
1360+
"by_col": np.array([1], dtype=dtype),
1361+
"on_col": [1],
1362+
"value": ["b"],
1363+
}
1364+
)
1365+
result = merge_asof(left, right, by="by_col", on="on_col")
1366+
expected = pd.DataFrame(
1367+
{
1368+
"by_col": np.array([1], dtype=dtype),
1369+
"on_col": [2],
1370+
"value_x": ["a"],
1371+
"value_y": ["b"],
1372+
}
1373+
)
1374+
tm.assert_frame_equal(result, expected)
1375+
13481376
def test_timedelta_tolerance_nearest(self, unit):
13491377
# GH 27642
13501378
if unit == "s":

0 commit comments

Comments
 (0)