Skip to content

Commit 996ed73

Browse files
committed
ENH: Generalization of RangeIndex to support floats. (pandas-dev#46484)
1 parent 05261fa commit 996ed73

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

pandas/core/indexes/float_range.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __getitem__(self, key):
5353
if isinstance(key, slice):
5454
if key.start is None and key.stop is None and key.step == -1:
5555
return float_range(
56-
start=self.stop - self.step,
56+
start=self.start + (len(self) - 1) * self.step,
5757
stop=self.start - self.step,
5858
step=-self.step,
5959
)
@@ -68,24 +68,18 @@ def __len__(self):
6868

6969
def __next__(self):
7070
self.current += self.step
71-
if self.current < self.stop:
71+
if self.current < self.stop and not np.isclose(self.current, self.stop):
7272
return self.current
7373
raise StopIteration
7474

75-
def index(self, key):
75+
def index(self, key) -> int:
7676
if key not in self:
7777
raise ValueError
7878
return round((key - self.start) / self.step)
7979

8080
@property
81-
def length(self):
82-
if self.step > 0:
83-
lo, hi = self.start, self.stop
84-
step = self.step
85-
else:
86-
hi, lo = self.start, self.stop
87-
step = -self.step
88-
if lo > hi or np.isclose(lo, hi):
89-
return 0
90-
else:
91-
return round((hi - lo - step) / step + 1)
81+
def length(self) -> int:
82+
estimate = (self.stop - self.start) // self.step
83+
if not np.isclose(self.start + estimate * self.step, self.stop):
84+
estimate += 1
85+
return estimate
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pandas import RangeIndex
2+
3+
RangeIndex(1, 5, dtype="float64")

0 commit comments

Comments
 (0)