Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2167,6 +2167,10 @@ def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]
# 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 @@ -1345,6 +1345,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