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

BUG: fix reindexing with a tz-aware index and method='nearest' #31511

Merged
merged 8 commits into from
Feb 3, 2020
5 changes: 2 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
kandersolar marked this conversation as resolved.
Show resolved Hide resolved
right_distances = abs(self[right_indexer] - target)

op = operator.lt if self.is_monotonic_increasing else operator.le
indexer = np.where(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
kandersolar marked this conversation as resolved.
Show resolved Hide resolved
idx = pd.date_range("2019-01-01", periods=5, tz="US/Eastern")
kandersolar marked this conversation as resolved.
Show resolved Hide resolved
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