Skip to content

Commit 64336ff

Browse files
authored
BUG: fix reindexing with a tz-aware index and method='nearest' (#31511)
1 parent 27f365d commit 64336ff

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Datetimelike
108108
- Bug in :class:`Timestamp` where constructing :class:`Timestamp` from ambiguous epoch time and calling constructor again changed :meth:`Timestamp.value` property (:issue:`24329`)
109109
- :meth:`DatetimeArray.searchsorted`, :meth:`TimedeltaArray.searchsorted`, :meth:`PeriodArray.searchsorted` not recognizing non-pandas scalars and incorrectly raising ``ValueError`` instead of ``TypeError`` (:issue:`30950`)
110110
- 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`)
111+
- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`)
111112

112113
Timedelta
113114
^^^^^^^^^
@@ -154,7 +155,6 @@ Indexing
154155
- Bug in :meth:`Series.at` and :meth:`DataFrame.at` not matching ``.loc`` behavior when looking up an integer in a :class:`Float64Index` (:issue:`31329`)
155156
- Bug in :meth:`PeriodIndex.is_monotonic` incorrectly returning ``True`` when containing leading ``NaT`` entries (:issue:`31437`)
156157
- Bug in :meth:`DatetimeIndex.get_loc` raising ``KeyError`` with converted-integer key instead of the user-passed key (:issue:`31425`)
157-
-
158158

159159
Missing
160160
^^^^^^^

pandas/core/indexes/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3073,9 +3073,8 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray:
30733073
left_indexer = self.get_indexer(target, "pad", limit=limit)
30743074
right_indexer = self.get_indexer(target, "backfill", limit=limit)
30753075

3076-
target = np.asarray(target)
3077-
left_distances = abs(self.values[left_indexer] - target)
3078-
right_distances = abs(self.values[right_indexer] - target)
3076+
left_distances = np.abs(self[left_indexer] - target)
3077+
right_distances = np.abs(self[right_indexer] - target)
30793078

30803079
op = operator.lt if self.is_monotonic_increasing else operator.le
30813080
indexer = np.where(

pandas/tests/frame/indexing/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,16 @@ def test_reindex_methods_nearest_special(self):
16021602
actual = df.reindex(target, method="nearest", tolerance=[0.5, 0.01, 0.4, 0.1])
16031603
tm.assert_frame_equal(expected, actual)
16041604

1605+
def test_reindex_nearest_tz(self, tz_aware_fixture):
1606+
# GH26683
1607+
tz = tz_aware_fixture
1608+
idx = pd.date_range("2019-01-01", periods=5, tz=tz)
1609+
df = pd.DataFrame({"x": list(range(5))}, index=idx)
1610+
1611+
expected = df.head(3)
1612+
actual = df.reindex(idx[:3], method="nearest")
1613+
tm.assert_frame_equal(expected, actual)
1614+
16051615
def test_reindex_frame_add_nat(self):
16061616
rng = date_range("1/1/2000 00:00:00", periods=10, freq="10s")
16071617
df = DataFrame({"A": np.random.randn(len(rng)), "B": rng})

0 commit comments

Comments
 (0)