Skip to content

REF: share code between Int64Index and UInt64Index #31129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 14 additions & 33 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class IntegerIndex(NumericIndex):
This is an abstract class for Int64Index, UInt64Index.
"""

_default_dtype: np.dtype

def __contains__(self, key) -> bool:
"""
Check if key is a float and has a decimal. If it has, return False.
Expand All @@ -243,26 +245,17 @@ def __contains__(self, key) -> bool:
except (OverflowError, TypeError, ValueError):
return False


class Int64Index(IntegerIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args

_typ = "int64index"
_can_hold_na = False
_engine_type = libindex.Int64Engine
_default_dtype = np.int64

@property
def inferred_type(self) -> str:
"""
Always 'integer' for ``Int64Index``
Always 'integer' for ``Int64Index`` and ``UInt64Index``
"""
return "integer"

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

@Appender(_index_shared_docs["_convert_scalar_indexer"])
def _convert_scalar_indexer(self, key, kind=None):
Expand All @@ -273,6 +266,15 @@ def _convert_scalar_indexer(self, key, kind=None):
key = self._maybe_cast_indexer(key)
return super()._convert_scalar_indexer(key, kind=kind)


class Int64Index(IntegerIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args

_typ = "int64index"
_can_hold_na = False
_engine_type = libindex.Int64Engine
_default_dtype = np.dtype(np.int64)

def _wrap_joined_index(self, joined, other):
name = get_op_result_name(self, other)
return Int64Index(joined, name=name)
Expand Down Expand Up @@ -307,28 +309,7 @@ class UInt64Index(IntegerIndex):
_typ = "uint64index"
_can_hold_na = False
_engine_type = libindex.UInt64Engine
_default_dtype = np.uint64

@property
def inferred_type(self) -> str:
"""
Always 'integer' for ``UInt64Index``
"""
return "integer"

@property
def asi8(self) -> np.ndarray:
# do not cache or you'll create a memory leak
return self.values.view("u8")

@Appender(_index_shared_docs["_convert_scalar_indexer"])
def _convert_scalar_indexer(self, key, kind=None):
assert kind in ["loc", "getitem", "iloc", None]

# don't coerce ilocs to integers
if kind != "iloc":
key = self._maybe_cast_indexer(key)
return super()._convert_scalar_indexer(key, kind=kind)
_default_dtype = np.dtype(np.uint64)

@Appender(_index_shared_docs["_convert_arr_indexer"])
def _convert_arr_indexer(self, keyarr):
Expand Down