Skip to content

Commit

Permalink
Backport PR #31511 on branch 1.0.x (BUG: fix reindexing with a tz-awa…
Browse files Browse the repository at this point in the history
…re index and method='nearest') (#32150)
  • Loading branch information
simonjayhawkins committed Feb 21, 2020
1 parent 6fcc6c3 commit b53a718
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Fixed regressions
Bug fixes
~~~~~~~~~

**Datetimelike**

- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`)

**Categorical**

- Fixed bug where :meth:`Categorical.from_codes` improperly raised a ``ValueError`` when passed nullable integer codes. (:issue:`31779`)
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2817,9 +2817,8 @@ def _get_nearest_indexer(self, target, limit, tolerance):
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 = 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(
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,16 @@ 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, 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)
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})
Expand Down

0 comments on commit b53a718

Please sign in to comment.