Skip to content

BUG: restore limit in RangeIndex.get_indexer #28671

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

Merged
merged 11 commits into from
Oct 1, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Interval
Indexing
^^^^^^^^

-
- Fix regression in :meth:`DataFrame.reindex` not following ``limit`` argument (:issue:`28631`).
-
-

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,10 @@ def get_loc(self, key, method=None, tolerance=None):

@Appender(_index_shared_docs["get_indexer"])
def get_indexer(self, target, method=None, limit=None, tolerance=None):
if not (method is None and tolerance is None and is_list_like(target)):
return super().get_indexer(target, method=method, tolerance=tolerance)
if com.any_not_none(method, tolerance, limit) or not is_list_like(target):
return super().get_indexer(
target, method=method, tolerance=tolerance, limit=limit
)

if self.step > 0:
start, stop, step = self.start, self.stop, self.step
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,22 @@ def test_reindex_frame_add_nat(self):
assert mask[-5:].all()
assert not mask[:-5].any()

def test_reindex_limit(self):
# GH 28631
data = [["A", "A", "A"], ["B", "B", "B"], ["C", "C", "C"], ["D", "D", "D"]]
exp_data = [
["A", "A", "A"],
["B", "B", "B"],
["C", "C", "C"],
["D", "D", "D"],
["D", "D", "D"],
[np.nan, np.nan, np.nan],
]
df = DataFrame(data)
result = df.reindex([0, 1, 2, 3, 4, 5], method="ffill", limit=1)
expected = DataFrame(exp_data)
tm.assert_frame_equal(result, expected)

def test_set_dataframe_column_ns_dtype(self):
x = DataFrame([datetime.now(), datetime.now()])
assert x[0].dtype == np.dtype("M8[ns]")
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ def test_get_indexer_backfill(self):
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
tm.assert_numpy_array_equal(indexer, expected)

def test_get_indexer_limit(self):
# GH 28631
idx = RangeIndex(4)
target = RangeIndex(6)
result = idx.get_indexer(target, method="pad", limit=1)
expected = np.array([0, 1, 2, 3, 3, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_join_outer(self):
# join with Int64Index
other = Int64Index(np.arange(25, 14, -1))
Expand Down