Skip to content

Commit

Permalink
Backport PR #55670 on branch 2.1.x (REGR: merge_asof raising TypeErro…
Browse files Browse the repository at this point in the history
…r for "by" with datelike dtypes) (#55676)

Backport PR #55670: REGR: merge_asof raising TypeError for "by" with datelike dtypes

Co-authored-by: Luke Manley <lukemanley@gmail.com>
  • Loading branch information
meeseeksmachine and lukemanley committed Oct 25, 2023
1 parent f9a4bef commit 96ecfec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.2.rst
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/reshape/merge.py
Expand Up @@ -2193,6 +2193,10 @@ def injection(obj: ArrayLike):
# 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
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/reshape/merge/test_merge_asof.py
Expand Up @@ -1341,6 +1341,34 @@ def test_by_mixed_tz_aware(self):
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":
Expand Down

0 comments on commit 96ecfec

Please sign in to comment.