1
1
from copy import copy
2
2
3
- from cython import Py_ssize_t
4
-
5
- from libc.stdlib cimport free, malloc
6
-
7
3
import numpy as np
8
4
9
5
cimport numpy as cnp
10
6
from numpy cimport int64_t, ndarray
11
7
12
8
cnp.import_array()
13
9
14
- from pandas._libs cimport util
10
+ from pandas._libs.util cimport is_array
15
11
16
12
from pandas._libs.lib import is_scalar, maybe_convert_objects
17
13
18
14
19
15
cpdef check_result_array(object obj, Py_ssize_t cnt):
20
16
21
- if (util. is_array(obj) or
17
+ if (is_array(obj) or
22
18
(isinstance (obj, list ) and len (obj) == cnt) or
23
19
getattr (obj, ' shape' , None ) == (cnt,)):
24
20
raise ValueError (' Must produce aggregated value' )
@@ -33,7 +29,7 @@ cdef class _BaseGrouper:
33
29
if (dummy.dtype != self .arr.dtype
34
30
and values.dtype != self .arr.dtype):
35
31
raise ValueError (' Dummy array must be same dtype' )
36
- if util. is_array(values) and not values.flags.contiguous:
32
+ if is_array(values) and not values.flags.contiguous:
37
33
# e.g. Categorical has no `flags` attribute
38
34
values = values.copy()
39
35
index = dummy.index.values
@@ -106,7 +102,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
106
102
self .f = f
107
103
108
104
values = series.values
109
- if util. is_array(values) and not values.flags.c_contiguous:
105
+ if is_array(values) and not values.flags.c_contiguous:
110
106
# e.g. Categorical has no `flags` attribute
111
107
values = values.copy(' C' )
112
108
self .arr = values
@@ -204,7 +200,7 @@ cdef class SeriesGrouper(_BaseGrouper):
204
200
self .f = f
205
201
206
202
values = series.values
207
- if util. is_array(values) and not values.flags.c_contiguous:
203
+ if is_array(values) and not values.flags.c_contiguous:
208
204
# e.g. Categorical has no `flags` attribute
209
205
values = values.copy(' C' )
210
206
self .arr = values
@@ -288,9 +284,9 @@ cpdef inline extract_result(object res, bint squeeze=True):
288
284
res = res._values
289
285
if squeeze and res.ndim == 1 and len (res) == 1 :
290
286
res = res[0 ]
291
- if hasattr (res, ' values' ) and util. is_array(res.values):
287
+ if hasattr (res, ' values' ) and is_array(res.values):
292
288
res = res.values
293
- if util. is_array(res):
289
+ if is_array(res):
294
290
if res.ndim == 0 :
295
291
res = res.item()
296
292
elif squeeze and res.ndim == 1 and len (res) == 1 :
@@ -304,7 +300,7 @@ cdef class Slider:
304
300
"""
305
301
cdef:
306
302
ndarray values, buf
307
- Py_ssize_t stride, orig_len, orig_stride
303
+ Py_ssize_t stride, orig_len
308
304
char * orig_data
309
305
310
306
def __init__ (self , ndarray values , ndarray buf ):
@@ -320,7 +316,6 @@ cdef class Slider:
320
316
321
317
self .orig_data = self .buf.data
322
318
self .orig_len = self .buf.shape[0 ]
323
- self .orig_stride = self .buf.strides[0 ]
324
319
325
320
self .buf.data = self .values.data
326
321
self .buf.strides[0 ] = self .stride
@@ -334,9 +329,8 @@ cdef class Slider:
334
329
335
330
cdef reset(self ):
336
331
337
- self .buf.shape[0 ] = self .orig_len
338
332
self .buf.data = self .orig_data
339
- self .buf.strides [0 ] = self .orig_stride
333
+ self .buf.shape [0 ] = self .orig_len
340
334
341
335
342
336
class InvalidApply (Exception ):
@@ -346,7 +340,6 @@ class InvalidApply(Exception):
346
340
def apply_frame_axis0 (object frame , object f , object names ,
347
341
const int64_t[:] starts , const int64_t[:] ends ):
348
342
cdef:
349
- BlockSlider slider
350
343
Py_ssize_t i, n = len (starts)
351
344
list results
352
345
object piece
@@ -357,16 +350,13 @@ def apply_frame_axis0(object frame, object f, object names,
357
350
358
351
results = []
359
352
360
- slider = BlockSlider(frame)
361
-
362
353
mutated = False
363
- item_cache = slider.dummy ._item_cache
354
+ item_cache = frame ._item_cache
364
355
try :
365
356
for i in range (n):
366
- slider.move(starts[i], ends[i])
367
357
368
358
item_cache.clear() # ugh
369
- chunk = slider.dummy
359
+ chunk = frame[starts[i]:ends[i]]
370
360
object .__setattr__ (chunk, ' name' , names[i])
371
361
372
362
try :
@@ -399,82 +389,6 @@ def apply_frame_axis0(object frame, object f, object names,
399
389
if require_slow_apply:
400
390
break
401
391
finally :
402
- slider.reset()
392
+ pass
403
393
404
394
return results, mutated
405
-
406
-
407
- cdef class BlockSlider:
408
- """
409
- Only capable of sliding on axis=0
410
- """
411
-
412
- cdef public:
413
- object frame, dummy, index
414
- int nblocks
415
- Slider idx_slider
416
- list blocks
417
-
418
- cdef:
419
- char ** base_ptrs
420
-
421
- def __init__ (self , object frame ):
422
- cdef:
423
- Py_ssize_t i
424
- object b
425
-
426
- self .frame = frame
427
- self .dummy = frame[:0 ]
428
- self .index = self .dummy.index
429
-
430
- self .blocks = [b.values for b in self .dummy._mgr.blocks]
431
-
432
- for x in self .blocks:
433
- util.set_array_not_contiguous(x)
434
-
435
- self .nblocks = len (self .blocks)
436
- # See the comment in indexes/base.py about _index_data.
437
- # We need this for EA-backed indexes that have a reference to a 1-d
438
- # ndarray like datetime / timedelta / period.
439
- self .idx_slider = Slider(
440
- self .frame.index._index_data, self .dummy.index._index_data)
441
-
442
- self .base_ptrs = < char ** > malloc(sizeof(char * ) * len (self .blocks))
443
- for i, block in enumerate (self .blocks):
444
- self .base_ptrs[i] = (< ndarray> block).data
445
-
446
- def __dealloc__ (self ):
447
- free(self .base_ptrs)
448
-
449
- cdef move(self , int start, int end):
450
- cdef:
451
- ndarray arr
452
- Py_ssize_t i
453
-
454
- # move blocks
455
- for i in range (self .nblocks):
456
- arr = self .blocks[i]
457
-
458
- # axis=1 is the frame's axis=0
459
- arr.data = self .base_ptrs[i] + arr.strides[1 ] * start
460
- arr.shape[1 ] = end - start
461
-
462
- # move and set the index
463
- self .idx_slider.move(start, end)
464
-
465
- object .__setattr__ (self .index, ' _index_data' , self .idx_slider.buf)
466
- self .index._engine.clear_mapping()
467
- self .index._cache.clear() # e.g. inferred_freq must go
468
-
469
- cdef reset(self ):
470
- cdef:
471
- ndarray arr
472
- Py_ssize_t i
473
-
474
- # reset blocks
475
- for i in range (self .nblocks):
476
- arr = self .blocks[i]
477
-
478
- # axis=1 is the frame's axis=0
479
- arr.data = self .base_ptrs[i]
480
- arr.shape[1 ] = 0
0 commit comments