Skip to content

Commit 7d6fc28

Browse files
committed
Only if index
1 parent ab72067 commit 7d6fc28

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

pandas/core/indexes/range.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
doc,
3030
)
3131

32+
from pandas.core.dtypes import missing
3233
from pandas.core.dtypes.common import (
3334
ensure_platform_int,
3435
ensure_python_int,
@@ -472,7 +473,7 @@ def _shallow_copy(self, values, name: Hashable = no_default):
472473
# GH 46675 & 43885: If values is equally spaced, return a
473474
# more memory-compact RangeIndex instead of Index with 64-bit dtype
474475
diff = values[1] - values[0]
475-
if diff != 0:
476+
if not missing.isna(diff) and diff != 0:
476477
maybe_range_indexer, remainder = np.divmod(values - values[0], diff)
477478
if (
478479
lib.is_range_indexer(maybe_range_indexer, len(maybe_range_indexer))
@@ -488,7 +489,8 @@ def _view(self) -> Self:
488489
return result
489490

490491
def _wrap_reindex_result(self, target, indexer, preserve_names: bool):
491-
target = self._shallow_copy(target._values)
492+
if target.dtype.kind == "i":
493+
target = self._shallow_copy(target._values)
492494
return super()._wrap_reindex_result(target, indexer, preserve_names)
493495

494496
@doc(Index.copy)

pandas/tests/indexing/test_loc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,10 +1204,7 @@ def test_loc_setitem_empty_append_raises(self):
12041204
data = [1, 2]
12051205
df = DataFrame(columns=["x", "y"])
12061206
df.index = df.index.astype(np.int64)
1207-
msg = (
1208-
rf"None of \[Index\(\[0, 1\], dtype='{np.dtype(int)}'\)\] "
1209-
r"are in the \[index\]"
1210-
)
1207+
msg = r"None of .*Index.* are in the \[index\]"
12111208
with pytest.raises(KeyError, match=msg):
12121209
df.loc[[0, 1], "x"] = data
12131210

0 commit comments

Comments
 (0)