@@ -24,7 +24,6 @@ cnp.import_array()
24
24
25
25
from pandas._libs.algos import ensure_int64
26
26
27
- from pandas._libs.arrays cimport NDArrayBacked
28
27
from pandas._libs.util cimport (
29
28
is_array,
30
29
is_integer_object,
@@ -639,14 +638,19 @@ def _unpickle_block(values, placement, ndim):
639
638
640
639
641
640
@ cython.freelist (64 )
642
- cdef class SharedBlock :
641
+ cdef class Block :
643
642
"""
644
643
Defining __init__ in a cython class significantly improves performance.
645
644
"""
646
645
cdef:
647
646
public BlockPlacement _mgr_locs
648
647
public BlockValuesRefs refs
649
648
readonly int ndim
649
+ # 2023-08-15 no apparent performance improvement from declaring values
650
+ # as ndarray in a type-special subclass (similar for NDArrayBacked).
651
+ # This might change if slice_block_rows can be optimized with something
652
+ # like https://github.com/numpy/numpy/issues/23934
653
+ public object values
650
654
651
655
def __cinit__ (
652
656
self ,
@@ -666,6 +670,8 @@ cdef class SharedBlock:
666
670
refs: BlockValuesRefs, optional
667
671
Ref tracking object or None if block does not have any refs.
668
672
"""
673
+ self .values = values
674
+
669
675
self ._mgr_locs = placement
670
676
self .ndim = ndim
671
677
if refs is None :
@@ -699,51 +705,7 @@ cdef class SharedBlock:
699
705
ndim = maybe_infer_ndim(self .values, self .mgr_locs)
700
706
self .ndim = ndim
701
707
702
-
703
- cdef class NumpyBlock(SharedBlock):
704
- cdef:
705
- public ndarray values
706
-
707
- def __cinit__ (
708
- self ,
709
- ndarray values ,
710
- BlockPlacement placement ,
711
- int ndim ,
712
- refs: BlockValuesRefs | None = None ,
713
- ):
714
- # set values here; the (implicit) call to SharedBlock.__cinit__ will
715
- # set placement, ndim and refs
716
- self .values = values
717
-
718
- cpdef NumpyBlock slice_block_rows(self , slice slicer):
719
- """
720
- Perform __getitem__-like specialized to slicing along index.
721
-
722
- Assumes self.ndim == 2
723
- """
724
- new_values = self .values[..., slicer]
725
- return type (self )(new_values, self ._mgr_locs, ndim = self .ndim, refs = self .refs)
726
-
727
-
728
- cdef class NDArrayBackedBlock(SharedBlock):
729
- """
730
- Block backed by NDArrayBackedExtensionArray
731
- """
732
- cdef public:
733
- NDArrayBacked values
734
-
735
- def __cinit__ (
736
- self ,
737
- NDArrayBacked values ,
738
- BlockPlacement placement ,
739
- int ndim ,
740
- refs: BlockValuesRefs | None = None ,
741
- ):
742
- # set values here; the (implicit) call to SharedBlock.__cinit__ will
743
- # set placement, ndim and refs
744
- self .values = values
745
-
746
- cpdef NDArrayBackedBlock slice_block_rows(self , slice slicer):
708
+ cpdef Block slice_block_rows(self , slice slicer):
747
709
"""
748
710
Perform __getitem__-like specialized to slicing along index.
749
711
@@ -753,22 +715,6 @@ cdef class NDArrayBackedBlock(SharedBlock):
753
715
return type (self )(new_values, self ._mgr_locs, ndim = self .ndim, refs = self .refs)
754
716
755
717
756
- cdef class Block(SharedBlock):
757
- cdef:
758
- public object values
759
-
760
- def __cinit__ (
761
- self ,
762
- object values ,
763
- BlockPlacement placement ,
764
- int ndim ,
765
- refs: BlockValuesRefs | None = None ,
766
- ):
767
- # set values here; the (implicit) call to SharedBlock.__cinit__ will
768
- # set placement, ndim and refs
769
- self .values = values
770
-
771
-
772
718
@ cython.freelist (64 )
773
719
cdef class BlockManager:
774
720
cdef:
@@ -811,7 +757,7 @@ cdef class BlockManager:
811
757
cdef:
812
758
intp_t blkno , i , j
813
759
cnp.npy_intp length = self .shape[0 ]
814
- SharedBlock blk
760
+ Block blk
815
761
BlockPlacement bp
816
762
ndarray[intp_t , ndim = 1 ] new_blknos, new_blklocs
817
763
@@ -901,7 +847,7 @@ cdef class BlockManager:
901
847
902
848
cdef BlockManager _slice_mgr_rows(self , slice slobj ):
903
849
cdef:
904
- SharedBlock blk, nb
850
+ Block blk, nb
905
851
BlockManager mgr
906
852
ndarray blknos, blklocs
907
853
@@ -945,18 +891,18 @@ cdef class BlockValuesRefs:
945
891
cdef:
946
892
public list referenced_blocks
947
893
948
- def __cinit__(self , blk: SharedBlock | None = None ) -> None:
894
+ def __cinit__(self , blk: Block | None = None ) -> None:
949
895
if blk is not None:
950
896
self.referenced_blocks = [weakref.ref(blk)]
951
897
else:
952
898
self.referenced_blocks = []
953
899
954
- def add_reference(self , blk: SharedBlock ) -> None:
900
+ def add_reference(self , blk: Block ) -> None:
955
901
"""Adds a new reference to our reference collection.
956
902
957
903
Parameters
958
904
----------
959
- blk: SharedBlock
905
+ blk : Block
960
906
The block that the new references should point to.
961
907
"""
962
908
self.referenced_blocks.append(weakref.ref(blk ))
0 commit comments