Skip to content

Commit 31a7f57

Browse files
jbrockmendeljreback
authored andcommitted
REF: share code between Int64Index and UInt64Index (pandas-dev#31129)
1 parent 7d28040 commit 31a7f57

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

pandas/core/indexes/numeric.py

+14-33
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class IntegerIndex(NumericIndex):
231231
This is an abstract class for Int64Index, UInt64Index.
232232
"""
233233

234+
_default_dtype: np.dtype
235+
234236
def __contains__(self, key) -> bool:
235237
"""
236238
Check if key is a float and has a decimal. If it has, return False.
@@ -243,26 +245,17 @@ def __contains__(self, key) -> bool:
243245
except (OverflowError, TypeError, ValueError):
244246
return False
245247

246-
247-
class Int64Index(IntegerIndex):
248-
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args
249-
250-
_typ = "int64index"
251-
_can_hold_na = False
252-
_engine_type = libindex.Int64Engine
253-
_default_dtype = np.int64
254-
255248
@property
256249
def inferred_type(self) -> str:
257250
"""
258-
Always 'integer' for ``Int64Index``
251+
Always 'integer' for ``Int64Index`` and ``UInt64Index``
259252
"""
260253
return "integer"
261254

262255
@property
263256
def asi8(self) -> np.ndarray:
264257
# do not cache or you'll create a memory leak
265-
return self.values.view("i8")
258+
return self.values.view(self._default_dtype)
266259

267260
@Appender(_index_shared_docs["_convert_scalar_indexer"])
268261
def _convert_scalar_indexer(self, key, kind=None):
@@ -273,6 +266,15 @@ def _convert_scalar_indexer(self, key, kind=None):
273266
key = self._maybe_cast_indexer(key)
274267
return super()._convert_scalar_indexer(key, kind=kind)
275268

269+
270+
class Int64Index(IntegerIndex):
271+
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args
272+
273+
_typ = "int64index"
274+
_can_hold_na = False
275+
_engine_type = libindex.Int64Engine
276+
_default_dtype = np.dtype(np.int64)
277+
276278
def _wrap_joined_index(self, joined, other):
277279
name = get_op_result_name(self, other)
278280
return Int64Index(joined, name=name)
@@ -307,28 +309,7 @@ class UInt64Index(IntegerIndex):
307309
_typ = "uint64index"
308310
_can_hold_na = False
309311
_engine_type = libindex.UInt64Engine
310-
_default_dtype = np.uint64
311-
312-
@property
313-
def inferred_type(self) -> str:
314-
"""
315-
Always 'integer' for ``UInt64Index``
316-
"""
317-
return "integer"
318-
319-
@property
320-
def asi8(self) -> np.ndarray:
321-
# do not cache or you'll create a memory leak
322-
return self.values.view("u8")
323-
324-
@Appender(_index_shared_docs["_convert_scalar_indexer"])
325-
def _convert_scalar_indexer(self, key, kind=None):
326-
assert kind in ["loc", "getitem", "iloc", None]
327-
328-
# don't coerce ilocs to integers
329-
if kind != "iloc":
330-
key = self._maybe_cast_indexer(key)
331-
return super()._convert_scalar_indexer(key, kind=kind)
312+
_default_dtype = np.dtype(np.uint64)
332313

333314
@Appender(_index_shared_docs["_convert_arr_indexer"])
334315
def _convert_arr_indexer(self, keyarr):

0 commit comments

Comments
 (0)