|
22 | 22 | index as libindex,
|
23 | 23 | lib,
|
24 | 24 | )
|
25 |
| -from pandas._libs.algos import unique_deltas |
26 | 25 | from pandas._libs.lib import no_default
|
27 | 26 | from pandas.compat.numpy import function as nv
|
28 | 27 | from pandas.util._decorators import (
|
@@ -469,15 +468,18 @@ def _shallow_copy(self, values, name: Hashable = no_default):
|
469 | 468 |
|
470 | 469 | if values.dtype.kind == "f":
|
471 | 470 | return Index(values, name=name, dtype=np.float64)
|
472 |
| - # GH 46675 & 43885: If values is equally spaced, return a |
473 |
| - # more memory-compact RangeIndex instead of Index with 64-bit dtype |
474 |
| - unique_diffs = unique_deltas(values) |
475 |
| - if len(unique_diffs) == 1 and unique_diffs[0] != 0: |
476 |
| - diff = unique_diffs[0] |
477 |
| - new_range = range(values[0], values[-1] + diff, diff) |
478 |
| - return type(self)._simple_new(new_range, name=name) |
479 |
| - else: |
480 |
| - return self._constructor._simple_new(values, name=name) |
| 471 | + if values.dtype.kind == "i" and values.ndim == 1 and len(values) > 1: |
| 472 | + # GH 46675 & 43885: If values is equally spaced, return a |
| 473 | + # more memory-compact RangeIndex instead of Index with 64-bit dtype |
| 474 | + diff = values[1] - values[0] |
| 475 | + if diff != 0: |
| 476 | + maybe_range_indexer, remainder = np.divmod(values - values[0], diff) |
| 477 | + if (remainder == 0).all() and lib.is_range_indexer( |
| 478 | + maybe_range_indexer, len(maybe_range_indexer) |
| 479 | + ): |
| 480 | + new_range = range(values[0], values[-1] + diff, diff) |
| 481 | + return type(self)._simple_new(new_range, name=name) |
| 482 | + return self._constructor._simple_new(values, name=name) |
481 | 483 |
|
482 | 484 | def _view(self) -> Self:
|
483 | 485 | result = type(self)._simple_new(self._range, name=self._name)
|
|
0 commit comments