Skip to content

Commit 8fbb323

Browse files
committed
rename and clarify hashing of the indexing key
1 parent 1b42ce0 commit 8fbb323

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

pandas/indexes/multi.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,6 @@ def is_monotonic(self):
681681
def is_unique(self):
682682
return not self.duplicated().any()
683683

684-
@cache_readonly
685-
def _hashed_values(self):
686-
""" return a uint64 ndarray of my hashed values """
687-
from pandas.tools.hashing import hash_tuples
688-
return hash_tuples(self)
689-
690684
@cache_readonly
691685
def _have_mixed_levels(self):
692686
""" return a boolean list indicated if we have mixed levels """
@@ -697,15 +691,35 @@ def _inferred_type_levels(self):
697691
""" return a list of the inferred types, one for each level """
698692
return [i.inferred_type for i in self.levels]
699693

700-
def _as_valid_indexing_key(self, key):
694+
@cache_readonly
695+
def _hashed_values(self):
696+
""" return a uint64 ndarray of my hashed values """
697+
from pandas.tools.hashing import hash_tuples
698+
return hash_tuples(self)
699+
700+
def _hashed_indexing_key(self, key):
701701
"""
702-
validate and return our key
702+
validate and return the hash for the provided key
703+
704+
*this is internal for use for the cython routines*
705+
706+
Paramters
707+
---------
708+
key : string or tuple
709+
710+
Returns
711+
-------
712+
np.uint64
713+
714+
Notes
715+
-----
703716
we need to stringify if we have mixed levels
704717
705-
this is internal for use for the cython routines
706718
"""
719+
from pandas.tools.hashing import hash_tuples
720+
707721
if not isinstance(key, tuple):
708-
return key
722+
return hash_tuples(key)
709723

710724
if not len(key) == self.nlevels:
711725
raise KeyError
@@ -714,8 +728,9 @@ def f(k, stringify):
714728
if stringify and not isinstance(k, compat.string_types):
715729
k = str(k)
716730
return k
717-
return tuple([f(k, stringify)
718-
for k, stringify in zip(key, self._have_mixed_levels)])
731+
key = tuple([f(k, stringify)
732+
for k, stringify in zip(key, self._have_mixed_levels)])
733+
return hash_tuples(key)
719734

720735
@deprecate_kwarg('take_last', 'keep', mapping={True: 'last',
721736
False: 'first'})

pandas/src/hashtable_class_helper.pxi.in

+3-9
Original file line numberDiff line numberDiff line change
@@ -864,22 +864,16 @@ cdef class MultiIndexHashTable(HashTable):
864864
khiter_t k
865865
uint64_t value
866866

867-
from pandas.tools.hashing import hash_tuples
868-
key = self.mi._as_valid_indexing_key(key)
869-
value = hash_tuples(key)
870-
k = kh_get_uint64(self.table, key)
867+
value = self.mi._hashed_indexing_key(key)
868+
k = kh_get_uint64(self.table, value)
871869
return k != self.table.n_buckets
872870

873871
cpdef get_item(self, object key):
874872
cdef:
875873
khiter_t k
876874
uint64_t value
877875

878-
from pandas.tools.hashing import hash_tuples
879-
key = self.mi._as_valid_indexing_key(key)
880-
value = hash_tuples(key)
881-
882-
# should check nlevels == len tuple
876+
value = self.mi._hashed_indexing_key(key)
883877
k = kh_get_uint64(self.table, value)
884878
if k != self.table.n_buckets:
885879
return self.table.vals[k]

0 commit comments

Comments
 (0)