forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.pyx
631 lines (483 loc) · 18.1 KB
/
index.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# cython: profile=False
from numpy cimport (ndarray, float64_t, int32_t, int64_t, uint8_t, uint64_t,
NPY_DATETIME, NPY_TIMEDELTA)
cimport cython
cimport numpy as cnp
cnp.import_array()
cnp.import_ufunc()
cimport util
import numpy as np
cimport tslib
from hashtable cimport HashTable
from pandas._libs import tslib, algos, hashtable as _hash
from pandas._libs.tslib import Timestamp, Timedelta
from datetime import datetime, timedelta
from datetime cimport (get_datetime64_value, _pydatetime_to_dts,
pandas_datetimestruct)
from tslibs.timezones cimport _get_utcoffset, _is_utc
from cpython cimport PyTuple_Check, PyList_Check
cdef extern from "datetime.h":
bint PyDateTime_Check(object o)
void PyDateTime_IMPORT()
cdef int64_t iNaT = util.get_nat()
from dateutil.tz import tzutc as _du_utc
import pytz
UTC = pytz.utc
PyDateTime_IMPORT
cdef extern from "Python.h":
int PySlice_Check(object)
cdef inline is_definitely_invalid_key(object val):
if PyTuple_Check(val):
try:
hash(val)
except TypeError:
return True
# we have a _data, means we are a NDFrame
return (PySlice_Check(val) or cnp.PyArray_Check(val)
or PyList_Check(val) or hasattr(val, '_data'))
def get_value_at(ndarray arr, object loc):
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_at(arr, loc))
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_at(arr, loc))
return util.get_value_at(arr, loc)
def set_value_at(ndarray arr, object loc, object val):
return util.set_value_at(arr, loc, val)
# Don't populate hash tables in monotonic indexes larger than this
_SIZE_CUTOFF = 1000000
cdef class IndexEngine:
cdef readonly:
object vgetter
HashTable mapping
bint over_size_threshold
cdef:
bint unique, monotonic_inc, monotonic_dec
bint need_monotonic_check, need_unique_check
def __init__(self, vgetter, n):
self.vgetter = vgetter
self.over_size_threshold = n >= _SIZE_CUTOFF
self.clear_mapping()
def __contains__(self, object val):
self._ensure_mapping_populated()
hash(val)
return val in self.mapping
cpdef get_value(self, ndarray arr, object key, object tz=None):
"""
arr : 1-dimensional ndarray
"""
cdef:
object loc
void* data_ptr
loc = self.get_loc(key)
if PySlice_Check(loc) or cnp.PyArray_Check(loc):
return arr[loc]
else:
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_at(arr, loc), tz=tz)
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_at(arr, loc))
return util.get_value_at(arr, loc)
cpdef set_value(self, ndarray arr, object key, object value):
"""
arr : 1-dimensional ndarray
"""
cdef:
object loc
void* data_ptr
loc = self.get_loc(key)
value = convert_scalar(arr, value)
if PySlice_Check(loc) or cnp.PyArray_Check(loc):
arr[loc] = value
else:
util.set_value_at(arr, loc, value)
cpdef get_loc(self, object val):
if is_definitely_invalid_key(val):
raise TypeError("'{val}' is an invalid key".format(val=val))
if self.over_size_threshold and self.is_monotonic_increasing:
if not self.is_unique:
return self._get_loc_duplicates(val)
values = self._get_index_values()
loc = _bin_search(values, val) # .searchsorted(val, side='left')
if loc >= len(values):
raise KeyError(val)
if util.get_value_at(values, loc) != val:
raise KeyError(val)
return loc
self._ensure_mapping_populated()
if not self.unique:
return self._get_loc_duplicates(val)
self._check_type(val)
try:
return self.mapping.get_item(val)
except (TypeError, ValueError):
raise KeyError(val)
cdef inline _get_loc_duplicates(self, object val):
cdef:
Py_ssize_t diff
if self.is_monotonic_increasing:
values = self._get_index_values()
left = values.searchsorted(val, side='left')
right = values.searchsorted(val, side='right')
diff = right - left
if diff == 0:
raise KeyError(val)
elif diff == 1:
return left
else:
return slice(left, right)
return self._maybe_get_bool_indexer(val)
cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t] indexer
ndarray[object] values
int count = 0
Py_ssize_t i, n
int last_true
values = np.array(self._get_index_values(), copy=False)
n = len(values)
result = np.empty(n, dtype=bool)
indexer = result.view(np.uint8)
for i in range(n):
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0
if count == 0:
raise KeyError(val)
if count == 1:
return last_true
return result
def sizeof(self, deep=False):
""" return the sizeof our mapping """
if not self.is_mapping_populated:
return 0
return self.mapping.sizeof(deep=deep)
def __sizeof__(self):
return self.sizeof()
property is_unique:
def __get__(self):
if self.need_unique_check:
self._do_unique_check()
return self.unique == 1
cdef inline _do_unique_check(self):
# this de-facto the same
self._ensure_mapping_populated()
property is_monotonic_increasing:
def __get__(self):
if self.need_monotonic_check:
self._do_monotonic_check()
return self.monotonic_inc == 1
property is_monotonic_decreasing:
def __get__(self):
if self.need_monotonic_check:
self._do_monotonic_check()
return self.monotonic_dec == 1
cdef inline _do_monotonic_check(self):
cdef object is_unique
try:
values = self._get_index_values()
self.monotonic_inc, self.monotonic_dec, is_unique = \
self._call_monotonic(values)
except TypeError:
self.monotonic_inc = 0
self.monotonic_dec = 0
is_unique = 0
self.need_monotonic_check = 0
# we can only be sure of uniqueness if is_unique=1
if is_unique:
self.unique = 1
self.need_unique_check = 0
cdef _get_index_values(self):
return self.vgetter()
def _call_monotonic(self, values):
raise NotImplementedError
cdef _make_hash_table(self, n):
raise NotImplementedError
cdef _check_type(self, object val):
hash(val)
property is_mapping_populated:
def __get__(self):
return self.mapping is not None
cdef inline _ensure_mapping_populated(self):
# this populates the mapping
# if its not already populated
# also satisfies the need_unique_check
if not self.is_mapping_populated:
values = self._get_index_values()
self.mapping = self._make_hash_table(len(values))
self.mapping.map_locations(values)
if len(self.mapping) == len(values):
self.unique = 1
self.need_unique_check = 0
def clear_mapping(self):
self.mapping = None
self.need_monotonic_check = 1
self.need_unique_check = 1
self.unique = 0
self.monotonic_inc = 0
self.monotonic_dec = 0
def get_indexer(self, values):
self._ensure_mapping_populated()
return self.mapping.lookup(values)
def get_indexer_non_unique(self, targets):
""" return an indexer suitable for takng from a non unique index
return the labels in the same order ast the target
and a missing indexer into the targets (which correspond
to the -1 indicies in the results """
cdef:
ndarray values, x
ndarray[int64_t] result, missing
set stargets
dict d = {}
object val
int count = 0, count_missing = 0
Py_ssize_t i, j, n, n_t, n_alloc
self._ensure_mapping_populated()
values = np.array(self._get_index_values(), copy=False)
stargets = set(targets)
n = len(values)
n_t = len(targets)
if n > 10000:
n_alloc = 10000
else:
n_alloc = n
result = np.empty(n_alloc, dtype=np.int64)
missing = np.empty(n_t, dtype=np.int64)
# form the set of the results (like ismember)
members = np.empty(n, dtype=np.uint8)
for i in range(n):
val = util.get_value_1d(values, i)
if val in stargets:
if val not in d:
d[val] = []
d[val].append(i)
for i in range(n_t):
val = util.get_value_1d(targets, i)
# found
if val in d:
for j in d[val]:
# realloc if needed
if count >= n_alloc:
n_alloc += 10000
result = np.resize(result, n_alloc)
result[count] = j
count += 1
# value not found
else:
if count >= n_alloc:
n_alloc += 10000
result = np.resize(result, n_alloc)
result[count] = -1
count += 1
missing[count_missing] = i
count_missing += 1
return result[0:count], missing[0:count_missing]
cdef Py_ssize_t _bin_search(ndarray values, object val) except -1:
cdef:
Py_ssize_t mid, lo = 0, hi = len(values) - 1
object pval
if hi >= 0 and val > util.get_value_at(values, hi):
return len(values)
while lo < hi:
mid = (lo + hi) // 2
pval = util.get_value_at(values, mid)
if val < pval:
hi = mid
elif val > pval:
lo = mid + 1
else:
while mid > 0 and val == util.get_value_at(values, mid - 1):
mid -= 1
return mid
if val <= util.get_value_at(values, mid):
return mid
else:
return mid + 1
_pad_functions = {
'object': algos.pad_object,
'int64': algos.pad_int64,
'float64': algos.pad_float64
}
_backfill_functions = {
'object': algos.backfill_object,
'int64': algos.backfill_int64,
'float64': algos.backfill_float64
}
cdef class DatetimeEngine(Int64Engine):
cdef _get_box_dtype(self):
return 'M8[ns]'
def __contains__(self, object val):
if self.over_size_threshold and self.is_monotonic_increasing:
if not self.is_unique:
return self._get_loc_duplicates(val)
values = self._get_index_values()
conv = _to_i8(val)
loc = values.searchsorted(conv, side='left')
return util.get_value_at(values, loc) == conv
self._ensure_mapping_populated()
return _to_i8(val) in self.mapping
cdef _get_index_values(self):
return self.vgetter().view('i8')
def _call_monotonic(self, values):
return algos.is_monotonic_int64(values, timelike=True)
cpdef get_loc(self, object val):
if is_definitely_invalid_key(val):
raise TypeError
# Welcome to the spaghetti factory
if self.over_size_threshold and self.is_monotonic_increasing:
if not self.is_unique:
val = _to_i8(val)
return self._get_loc_duplicates(val)
values = self._get_index_values()
try:
conv = _to_i8(val)
loc = values.searchsorted(conv, side='left')
except TypeError:
self._date_check_type(val)
raise KeyError(val)
if loc == len(values) or util.get_value_at(values, loc) != conv:
raise KeyError(val)
return loc
self._ensure_mapping_populated()
if not self.unique:
val = _to_i8(val)
return self._get_loc_duplicates(val)
try:
return self.mapping.get_item(val.value)
except KeyError:
raise KeyError(val)
except AttributeError:
pass
try:
val = _to_i8(val)
return self.mapping.get_item(val)
except (TypeError, ValueError):
self._date_check_type(val)
raise KeyError(val)
cdef inline _date_check_type(self, object val):
hash(val)
if not util.is_integer_object(val):
raise KeyError(val)
def get_indexer(self, values):
self._ensure_mapping_populated()
if values.dtype != self._get_box_dtype():
return np.repeat(-1, len(values)).astype('i4')
values = np.asarray(values).view('i8')
return self.mapping.lookup(values)
def get_pad_indexer(self, other, limit=None):
if other.dtype != self._get_box_dtype():
return np.repeat(-1, len(other)).astype('i4')
other = np.asarray(other).view('i8')
return algos.pad_int64(self._get_index_values(), other,
limit=limit)
def get_backfill_indexer(self, other, limit=None):
if other.dtype != self._get_box_dtype():
return np.repeat(-1, len(other)).astype('i4')
other = np.asarray(other).view('i8')
return algos.backfill_int64(self._get_index_values(), other,
limit=limit)
cdef class TimedeltaEngine(DatetimeEngine):
cdef _get_box_dtype(self):
return 'm8[ns]'
cpdef convert_scalar(ndarray arr, object value):
# we don't turn integers
# into datetimes/timedeltas
# we don't turn bools into int/float/complex
if arr.descr.type_num == NPY_DATETIME:
if isinstance(value, np.ndarray):
pass
elif isinstance(value, datetime):
return Timestamp(value).value
elif value is None or value != value:
return iNaT
elif util.is_string_object(value):
return Timestamp(value).value
raise ValueError("cannot set a Timestamp with a non-timestamp")
elif arr.descr.type_num == NPY_TIMEDELTA:
if isinstance(value, np.ndarray):
pass
elif isinstance(value, timedelta):
return Timedelta(value).value
elif value is None or value != value:
return iNaT
elif util.is_string_object(value):
return Timedelta(value).value
raise ValueError("cannot set a Timedelta with a non-timedelta")
if (issubclass(arr.dtype.type, (np.integer, np.floating, np.complex)) and
not issubclass(arr.dtype.type, np.bool_)):
if util.is_bool_object(value):
raise ValueError('Cannot assign bool to float/integer series')
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
if util.is_float_object(value) and value != value:
raise ValueError('Cannot assign nan to integer series')
return value
cdef inline _to_i8(object val):
cdef pandas_datetimestruct dts
try:
return val.value
except AttributeError:
if util.is_datetime64_object(val):
return get_datetime64_value(val)
elif PyDateTime_Check(val):
tzinfo = getattr(val, 'tzinfo', None)
# Save the original date value so we can get the utcoffset from it.
ival = _pydatetime_to_dts(val, &dts)
if tzinfo is not None and not _is_utc(tzinfo):
offset = _get_utcoffset(tzinfo, val)
ival -= tslib._delta_to_nanoseconds(offset)
return ival
return val
cdef class MultiIndexObjectEngine(ObjectEngine):
"""
provide the same interface as the MultiIndexEngine
but use the IndexEngine for computation
This provides good performance with samller MI's
"""
def get_indexer(self, values):
# convert a MI to an ndarray
if hasattr(values, 'values'):
values = values.values
return super(MultiIndexObjectEngine, self).get_indexer(values)
cpdef get_loc(self, object val):
# convert a MI to an ndarray
if hasattr(val, 'values'):
val = val.values
return super(MultiIndexObjectEngine, self).get_loc(val)
cdef class MultiIndexHashEngine(ObjectEngine):
"""
Use a hashing based MultiIndex impl
but use the IndexEngine for computation
This provides good performance with larger MI's
"""
def _call_monotonic(self, object mi):
# defer these back to the mi iteself
return (mi.is_monotonic_increasing,
mi.is_monotonic_decreasing,
mi.is_unique)
def get_backfill_indexer(self, other, limit=None):
# we coerce to ndarray-of-tuples
values = np.array(self._get_index_values())
return algos.backfill_object(values, other, limit=limit)
def get_pad_indexer(self, other, limit=None):
# we coerce to ndarray-of-tuples
values = np.array(self._get_index_values())
return algos.pad_object(values, other, limit=limit)
cpdef get_loc(self, object val):
if is_definitely_invalid_key(val):
raise TypeError("'{val}' is an invalid key".format(val=val))
self._ensure_mapping_populated()
if not self.unique:
return self._get_loc_duplicates(val)
try:
return self.mapping.get_item(val)
except TypeError:
raise KeyError(val)
def get_indexer(self, values):
self._ensure_mapping_populated()
return self.mapping.lookup(values)
cdef _make_hash_table(self, n):
return _hash.MultiIndexHashTable(n)
# Generated from template.
include "index_class_helper.pxi"