Skip to content

Commit 96ecfec

Browse files
Backport PR #55670 on branch 2.1.x (REGR: merge_asof raising TypeError for "by" with datelike dtypes) (#55676)
Backport PR #55670: REGR: merge_asof raising TypeError for "by" with datelike dtypes Co-authored-by: Luke Manley <[email protected]>
1 parent f9a4bef commit 96ecfec

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
@@ -2193,6 +2193,10 @@ def injection(obj: ArrayLike):
21932193
# TODO: conversions for EAs that can be no-copy.
21942194
lbv = np.asarray(lbv)
21952195
rbv = np.asarray(rbv)
2196+
if needs_i8_conversion(lbv.dtype):
2197+
lbv = lbv.view("i8")
2198+
if needs_i8_conversion(rbv.dtype):
2199+
rbv = rbv.view("i8")
21962200
else:
21972201
# We get here with non-ndarrays in test_merge_by_col_tz_aware
21982202
# 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
@@ -1341,6 +1341,34 @@ def test_by_mixed_tz_aware(self):
13411341
expected["value_y"] = np.array([np.nan], dtype=object)
13421342
tm.assert_frame_equal(result, expected)
13431343

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

0 commit comments

Comments
 (0)