9
9
from pandas .lib import is_bool_array
10
10
from pandas .types .generic import ABCIndexClass , ABCSeries , ABCDataFrame
11
11
from pandas .types .common import (is_categorical_dtype , is_numeric_dtype ,
12
- is_datetime64_dtype , is_timedelta64_dtype )
12
+ is_datetime64_dtype , is_timedelta64_dtype ,
13
+ is_list_like )
13
14
14
15
# 16 byte long hashing key
15
16
_default_hash_key = '0123456789123456'
16
17
17
18
18
19
def _combine_hash_arrays (arrays , num_items ):
19
- "Should be the same as CPython's tupleobject.c"
20
- first = next (arrays )
20
+ """
21
+ Parameters
22
+ ----------
23
+ arrays : generator
24
+ num_items : int
25
+
26
+ Should be the same as CPython's tupleobject.c
27
+ """
28
+ try :
29
+ first = next (arrays )
30
+ except StopIteration :
31
+ return np .array ([], dtype = np .uint64 )
32
+
21
33
arrays = itertools .chain ([first ], arrays )
22
34
23
- mult = np .zeros_like ( first ) + np . uint64 (1000003 )
35
+ mult = np .uint64 (1000003 )
24
36
out = np .zeros_like (first ) + np .uint64 (0x345678 )
25
37
for i , a in enumerate (arrays ):
26
38
inverse_i = num_items - i
@@ -135,11 +147,11 @@ def _hash_lists(vals, encoding='utf8', hash_key=None):
135
147
136
148
def hash_tuples (vals , encoding = 'utf8' , hash_key = None ):
137
149
"""
138
- Hash an MultiIndex / array_of_tuples efficiently
150
+ Hash an MultiIndex / list-of-tuples efficiently
139
151
140
152
Parameters
141
153
----------
142
- vals : MultiIndex, ndarray of tuples, or single tuple
154
+ vals : MultiIndex, list-of- tuples, or single tuple
143
155
encoding : string, default 'utf8'
144
156
hash_key : string key to encode, default to _default_hash_key
145
157
@@ -152,6 +164,8 @@ def hash_tuples(vals, encoding='utf8', hash_key=None):
152
164
if isinstance (vals , tuple ):
153
165
vals = [vals ]
154
166
is_tuple = True
167
+ elif not is_list_like (vals ):
168
+ raise TypeError ("must be convertible to a list-of-tuples" )
155
169
156
170
if not isinstance (vals , MultiIndex ):
157
171
vals = MultiIndex .from_tuples (vals )
0 commit comments