Skip to content

Commit

Permalink
Use step
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Mar 13, 2024
1 parent 1e98a41 commit 0b484bd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def is_range_indexer(
left: np.ndarray,
n: int, # np.ndarray[np.int64, ndim=1]
) -> bool: ...
def is_range(
def is_sequence_range(
sequence: np.ndarray,
diff: int, # np.ndarray[np.int64, ndim=1]
step: int, # np.ndarray[np.int64, ndim=1]
) -> bool: ...
8 changes: 4 additions & 4 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -680,19 +680,19 @@ def is_range_indexer(ndarray[int6432_t, ndim=1] left, Py_ssize_t n) -> bool:

@cython.wraparound(False)
@cython.boundscheck(False)
def is_range(ndarray[int6432_t, ndim=1] sequence, int64_t diff) -> bool:
def is_sequence_range(ndarray[intp_t, ndim=1] sequence, int64_t step) -> bool:
"""
Check if sequence is equivalent to a range with the specified diff.
Check if sequence is equivalent to a range with the specified step.
"""
cdef:
Py_ssize_t i, n = len(sequence)

if diff == 0:
if step == 0:
return False

for i in range(n):

if sequence[i] != sequence[0] + i * diff:
if sequence[i] != sequence[0] + i * step:
return False

return True
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def _shallow_copy(self, values, name: Hashable = no_default):
new_range = range(start, start + self.step, self.step)
return type(self)._simple_new(new_range, name=name)
diff = values[1] - values[0]
if not missing.isna(diff) and lib.is_range(values, diff):
if not missing.isna(diff) and lib.is_sequence_range(values, diff):
new_range = range(values[0], values[-1] + diff, diff)
return type(self)._simple_new(new_range, name=name)
return self._constructor._simple_new(values, name=name)
Expand Down

0 comments on commit 0b484bd

Please sign in to comment.