From e259bedaeebad1949793f2a3395080043e791240 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 30 Nov 2019 14:31:35 -0800 Subject: [PATCH 1/3] BUG: merge_asof with tz_aware left index and right column --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/core/reshape/merge.py | 2 +- pandas/tests/reshape/merge/test_merge_asof.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 3f8d9d3916797..1998abdd8e207 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -664,7 +664,7 @@ Reshaping - Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`) - Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`) - Bug in :func:`melt` where supplying mixed strings and numeric values for ``id_vars`` or ``value_vars`` would incorrectly raise a ``ValueError`` (:issue:`29718`) - +- Bug in :func:`merge_asof` merging on a tz-aware ``left_index`` and ``right_on`` a tz-aware column (:issue:`29864`) Sparse ^^^^^^ diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index fdd31b3b7c022..d671fff568891 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1027,7 +1027,7 @@ def _get_merge_keys(self): ) ] else: - left_keys = [self.left.index.values] + left_keys = [self.left.index._values] if left_drop: self.left = self.left._drop_labels_or_levels(left_drop) diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index e12aad870f1c1..824d16bc79550 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -1303,3 +1303,21 @@ def test_int_type_tolerance(self, any_int_dtype): result = pd.merge_asof(left, right, on="a", tolerance=10) tm.assert_frame_equal(result, expected) + + def test_left_index_right_on_column_tz(self): + # GH 29864 + index = pd.date_range("2019-10-01", freq="30min", periods=5, tz="UTC") + left = pd.DataFrame([0.9, 0.8, 0.7, 0.6], columns=["xyz"], index=index[1:]) + right = pd.DataFrame({"from_date": index, "abc": [2.46] * 4 + [2.19]}) + result = pd.merge_asof( + left=left, right=right, left_index=True, right_on=["from_date"] + ) + expected = pd.DataFrame( + { + "xyz": [0.9, 0.8, 0.7, 0.6], + "from_date": index[1:], + "abc": [2.46] * 3 + [2.19], + }, + index=pd.Index([1, 2, 3, 4]), + ) + tm.assert_frame_equal(result, expected) From 19ffe0c0a115ecb2a54a7f355ad3c180f0d9dcc4 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 30 Nov 2019 14:33:20 -0800 Subject: [PATCH 2/3] Add space after whatsnew note --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 1998abdd8e207..392089c2bb6d2 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -665,6 +665,7 @@ Reshaping - Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`) - Bug in :func:`melt` where supplying mixed strings and numeric values for ``id_vars`` or ``value_vars`` would incorrectly raise a ``ValueError`` (:issue:`29718`) - Bug in :func:`merge_asof` merging on a tz-aware ``left_index`` and ``right_on`` a tz-aware column (:issue:`29864`) +- Sparse ^^^^^^ From 39781d6967335cf3209d6040a54ea4faa9ed3681 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 30 Nov 2019 18:14:05 -0800 Subject: [PATCH 3/3] test opposite case --- pandas/tests/reshape/merge/test_merge_asof.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index 824d16bc79550..b2e764c5463fa 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -1304,7 +1304,7 @@ def test_int_type_tolerance(self, any_int_dtype): result = pd.merge_asof(left, right, on="a", tolerance=10) tm.assert_frame_equal(result, expected) - def test_left_index_right_on_column_tz(self): + def test_merge_index_column_tz(self): # GH 29864 index = pd.date_range("2019-10-01", freq="30min", periods=5, tz="UTC") left = pd.DataFrame([0.9, 0.8, 0.7, 0.6], columns=["xyz"], index=index[1:]) @@ -1321,3 +1321,16 @@ def test_left_index_right_on_column_tz(self): index=pd.Index([1, 2, 3, 4]), ) tm.assert_frame_equal(result, expected) + + result = pd.merge_asof( + left=right, right=left, right_index=True, left_on=["from_date"] + ) + expected = pd.DataFrame( + { + "from_date": index, + "abc": [2.46] * 4 + [2.19], + "xyz": [np.nan, 0.9, 0.8, 0.7, 0.6], + }, + index=pd.Index([0, 1, 2, 3, 4]), + ) + tm.assert_frame_equal(result, expected)