From dcaac8bf4ca4b7197d89a4e97fd7b8333dac7698 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Fri, 31 Jan 2020 11:13:11 -0700 Subject: [PATCH 1/4] BUG: fix reindexing a tz-aware index with method='nearest' (GH26683) --- pandas/core/indexes/base.py | 5 ++--- pandas/tests/frame/indexing/test_indexing.py | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 22ba317e78e63..b6fd5d31c03e8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3061,9 +3061,8 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray: left_indexer = self.get_indexer(target, "pad", limit=limit) right_indexer = self.get_indexer(target, "backfill", limit=limit) - target = np.asarray(target) - left_distances = abs(self.values[left_indexer] - target) - right_distances = abs(self.values[right_indexer] - target) + left_distances = abs(self[left_indexer] - target) + right_distances = abs(self[right_indexer] - target) op = operator.lt if self.is_monotonic_increasing else operator.le indexer = np.where( diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index cbb9dd09bbede..7a0a3aca9eb51 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1599,6 +1599,14 @@ def test_reindex_methods_nearest_special(self): actual = df.reindex(target, method="nearest", tolerance=[0.5, 0.01, 0.4, 0.1]) tm.assert_frame_equal(expected, actual) + def test_reindex_nearest_tz(self): + idx = pd.date_range("2019-01-01", periods=5, tz="US/Eastern") + df = pd.DataFrame({"x": list(range(5))}, index=idx) + + expected = df.head(3) + actual = df.reindex(idx[:3], method="nearest") + tm.assert_frame_equal(expected, actual) + def test_reindex_frame_add_nat(self): rng = date_range("1/1/2000 00:00:00", periods=10, freq="10s") df = DataFrame({"A": np.random.randn(len(rng)), "B": rng}) From 9881cae436e6c9ed07ed3bec99c7470d51f79a9e Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Fri, 31 Jan 2020 12:51:02 -0700 Subject: [PATCH 2/4] use tz_aware_fixture, link GH issue --- pandas/tests/frame/indexing/test_indexing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 7a0a3aca9eb51..e89b5a47a0161 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1599,8 +1599,10 @@ def test_reindex_methods_nearest_special(self): actual = df.reindex(target, method="nearest", tolerance=[0.5, 0.01, 0.4, 0.1]) tm.assert_frame_equal(expected, actual) - def test_reindex_nearest_tz(self): - idx = pd.date_range("2019-01-01", periods=5, tz="US/Eastern") + def test_reindex_nearest_tz(self, tz_aware_fixture): + # GH26683 + tz = tz_aware_fixture + idx = pd.date_range("2019-01-01", periods=5, tz=tz) df = pd.DataFrame({"x": list(range(5))}, index=idx) expected = df.head(3) From 933cec6b7b0f9ba787ba6111071e0c423fac92b3 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Fri, 31 Jan 2020 13:43:34 -0700 Subject: [PATCH 3/4] Update v1.1.0.rst --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e07a8fa0469f4..bdf363d4d57a2 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -150,7 +150,7 @@ Indexing - Bug in :meth:`PeriodIndex.get_loc` treating higher-resolution strings differently from :meth:`PeriodIndex.get_value` (:issue:`31172`) - Bug in :meth:`Series.at` and :meth:`DataFrame.at` not matching ``.loc`` behavior when looking up an integer in a :class:`Float64Index` (:issue:`31329`) - Bug in :meth:`PeriodIndex.is_monotonic` incorrectly returning ``True`` when containing leading ``NaT`` entries (:issue:`31437`) -- +- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`) Missing ^^^^^^^ From a9d2b27347d3cb93c668d85218a168a17f5332da Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Sun, 2 Feb 2020 18:10:34 -0700 Subject: [PATCH 4/4] address gh comments --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/indexes/base.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index bdf363d4d57a2..26361438639e7 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -105,6 +105,7 @@ Datetimelike - Bug in :class:`Timestamp` where constructing :class:`Timestamp` from ambiguous epoch time and calling constructor again changed :meth:`Timestamp.value` property (:issue:`24329`) - :meth:`DatetimeArray.searchsorted`, :meth:`TimedeltaArray.searchsorted`, :meth:`PeriodArray.searchsorted` not recognizing non-pandas scalars and incorrectly raising ``ValueError`` instead of ``TypeError`` (:issue:`30950`) - Bug in :class:`Timestamp` where constructing :class:`Timestamp` with dateutil timezone less than 128 nanoseconds before daylight saving time switch from winter to summer would result in nonexistent time (:issue:`31043`) +- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`) Timedelta ^^^^^^^^^ @@ -150,7 +151,6 @@ Indexing - Bug in :meth:`PeriodIndex.get_loc` treating higher-resolution strings differently from :meth:`PeriodIndex.get_value` (:issue:`31172`) - Bug in :meth:`Series.at` and :meth:`DataFrame.at` not matching ``.loc`` behavior when looking up an integer in a :class:`Float64Index` (:issue:`31329`) - Bug in :meth:`PeriodIndex.is_monotonic` incorrectly returning ``True`` when containing leading ``NaT`` entries (:issue:`31437`) -- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`) Missing ^^^^^^^ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b6fd5d31c03e8..0be5b7742f136 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3061,8 +3061,8 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray: left_indexer = self.get_indexer(target, "pad", limit=limit) right_indexer = self.get_indexer(target, "backfill", limit=limit) - left_distances = abs(self[left_indexer] - target) - right_distances = abs(self[right_indexer] - target) + left_distances = np.abs(self[left_indexer] - target) + right_distances = np.abs(self[right_indexer] - target) op = operator.lt if self.is_monotonic_increasing else operator.le indexer = np.where(