Skip to content

CLN: cleaned RangeIndex._min_fitting_element #12113

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import warnings
import operator
from functools import partial
from math import ceil, floor

from sys import getsizeof

import numpy as np
Expand Down Expand Up @@ -4267,16 +4265,14 @@ def intersection(self, other):
return new_index

def _min_fitting_element(self, lower_limit):
"""Returns the value of the smallest element greater than the limit"""
round = ceil if self._step > 0 else floor
no_steps = round((float(lower_limit) - self._start) / self._step)
return self._start + self._step * no_steps
"""Returns the smallest element greater than or equal to the limit"""
no_steps = -(-(lower_limit - self._start) // abs(self._step))
return self._start + abs(self._step) * no_steps

def _max_fitting_element(self, upper_limit):
"""Returns the value of the largest element smaller than the limit"""
round = floor if self._step > 0 else ceil
no_steps = round((float(upper_limit) - self._start) / self._step)
return self._start + self._step * no_steps
"""Returns the largest element smaller than or equal to the limit"""
no_steps = (upper_limit - self._start) // abs(self._step)
return self._start + abs(self._step) * no_steps

def _extended_gcd(self, a, b):
"""
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4266,6 +4266,11 @@ def test_min_fitting_element(self):
result = RangeIndex(5, 0, -1)._min_fitting_element(1)
self.assertEqual(1, result)

big_num = 500000000000000000000000

result = RangeIndex(5, big_num * 2, 1)._min_fitting_element(big_num)
self.assertEqual(big_num, result)

def test_max_fitting_element(self):
result = RangeIndex(0, 20, 2)._max_fitting_element(17)
self.assertEqual(16, result)
Expand All @@ -4279,6 +4284,11 @@ def test_max_fitting_element(self):
result = RangeIndex(5, 0, -1)._max_fitting_element(4)
self.assertEqual(4, result)

big_num = 500000000000000000000000

result = RangeIndex(5, big_num * 2, 1)._max_fitting_element(big_num)
self.assertEqual(big_num, result)

def test_pickle_compat_construction(self):
# RangeIndex() is a valid constructor
pass
Expand Down