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
^^^^^^^^

-
- :meth: `DataFrame.reindex` not following limit (:issue:`28631`).
-
-

Expand Down
11 changes: 9 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,15 @@ 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 not (
method is None
and tolerance is None
and limit is None
and 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
22 changes: 22 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,25 @@ def test_engineless_lookup(self):
assert idx.get_loc("a") == -1

assert "_engine" in idx._cache

def test_limit(self):
# GH 28631
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn’t belong here
rather with other dataframe reindex tests

look in tests/frame

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, move to tests/frame/test_indexing

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 = pd.DataFrame(data)
result = df.reindex([0, 1, 2, 3, 4, 5], method="ffill", limit=1)
expected = pd.DataFrame(exp_data)
tm.assert_frame_equal(result, expected)

idx = pd.Index(range(4))
target = pd.Index([0, 1, 2, 3, 4, 5])
arr = idx.get_indexer(target, method="pad", limit=1)
expected_arr = np.array([0, 1, 2, 3, 3, -1])
assert (arr == expected_arr).all()