@@ -135,22 +135,16 @@ class BaseBlockManager(DataManager):
135
135
This is *not* a public API class
136
136
"""
137
137
138
- __slots__ = [
139
- "axes" ,
140
- "blocks" ,
141
- "_known_consolidated" ,
142
- "_is_consolidated" ,
143
- "_blknos" ,
144
- "_blklocs" ,
145
- ]
138
+ __slots__ = ()
146
139
147
140
_blknos : np .ndarray
148
141
_blklocs : np .ndarray
149
142
blocks : tuple [Block , ...]
150
143
axes : list [Index ]
151
144
152
- # Non-trivially faster than a property
153
145
ndim : int
146
+ _known_consolidated : bool
147
+ _is_consolidated : bool
154
148
155
149
def __init__ (self , blocks , axes , verify_integrity = True ):
156
150
raise NotImplementedError
@@ -276,57 +270,6 @@ def arrays(self) -> list[ArrayLike]:
276
270
"""
277
271
return [blk .values for blk in self .blocks ]
278
272
279
- def __getstate__ (self ):
280
- block_values = [b .values for b in self .blocks ]
281
- block_items = [self .items [b .mgr_locs .indexer ] for b in self .blocks ]
282
- axes_array = list (self .axes )
283
-
284
- extra_state = {
285
- "0.14.1" : {
286
- "axes" : axes_array ,
287
- "blocks" : [
288
- {"values" : b .values , "mgr_locs" : b .mgr_locs .indexer }
289
- for b in self .blocks
290
- ],
291
- }
292
- }
293
-
294
- # First three elements of the state are to maintain forward
295
- # compatibility with 0.13.1.
296
- return axes_array , block_values , block_items , extra_state
297
-
298
- def __setstate__ (self , state ):
299
- def unpickle_block (values , mgr_locs , ndim : int ) -> Block :
300
- # TODO(EA2D): ndim would be unnecessary with 2D EAs
301
- # older pickles may store e.g. DatetimeIndex instead of DatetimeArray
302
- values = extract_array (values , extract_numpy = True )
303
- return new_block (values , placement = mgr_locs , ndim = ndim )
304
-
305
- if isinstance (state , tuple ) and len (state ) >= 4 and "0.14.1" in state [3 ]:
306
- state = state [3 ]["0.14.1" ]
307
- self .axes = [ensure_index (ax ) for ax in state ["axes" ]]
308
- ndim = len (self .axes )
309
-
310
- for blk in state ["blocks" ]:
311
- vals = blk ["values" ]
312
- # older versions may hold e.g. DatetimeIndex instead of DTA
313
- vals = extract_array (vals , extract_numpy = True )
314
- blk ["values" ] = ensure_block_shape (vals , ndim = ndim )
315
-
316
- self .blocks = tuple (
317
- unpickle_block (b ["values" ], b ["mgr_locs" ], ndim = ndim )
318
- for b in state ["blocks" ]
319
- )
320
- else :
321
- raise NotImplementedError ("pre-0.14.1 pickles are no longer supported" )
322
-
323
- self ._post_setstate ()
324
-
325
- def _post_setstate (self ) -> None :
326
- self ._is_consolidated = False
327
- self ._known_consolidated = False
328
- self ._rebuild_blknos_and_blklocs ()
329
-
330
273
def __repr__ (self ) -> str :
331
274
output = type (self ).__name__
332
275
for i , ax in enumerate (self .axes ):
@@ -823,7 +766,7 @@ def consolidate(self: T) -> T:
823
766
if self .is_consolidated ():
824
767
return self
825
768
826
- bm = type (self )(self .blocks , self .axes )
769
+ bm = type (self )(self .blocks , self .axes , verify_integrity = False )
827
770
bm ._is_consolidated = False
828
771
bm ._consolidate_inplace ()
829
772
return bm
@@ -1079,7 +1022,7 @@ def take(self: T, indexer, axis: int = 1, verify: bool = True) -> T:
1079
1022
)
1080
1023
1081
1024
1082
- class BlockManager (BaseBlockManager ):
1025
+ class BlockManager (libinternals . BlockManager , BaseBlockManager ):
1083
1026
"""
1084
1027
BaseBlockManager that holds 2D blocks.
1085
1028
"""
@@ -1095,27 +1038,18 @@ def __init__(
1095
1038
axes : Sequence [Index ],
1096
1039
verify_integrity : bool = True ,
1097
1040
):
1098
- self .axes = [ensure_index (ax ) for ax in axes ]
1099
- self .blocks : tuple [Block , ...] = tuple (blocks )
1100
-
1101
- for block in blocks :
1102
- if self .ndim != block .ndim :
1103
- raise AssertionError (
1104
- f"Number of Block dimensions ({ block .ndim } ) must equal "
1105
- f"number of axes ({ self .ndim } )"
1106
- )
1107
1041
1108
1042
if verify_integrity :
1109
- self . _verify_integrity ( )
1043
+ assert all ( isinstance ( x , Index ) for x in axes )
1110
1044
1111
- # Populate known_consolidate, blknos, and blklocs lazily
1112
- self ._known_consolidated = False
1113
- # error: Incompatible types in assignment (expression has type "None",
1114
- # variable has type "ndarray")
1115
- self . _blknos = None # type: ignore[assignment]
1116
- # error: Incompatible types in assignment (expression has type "None",
1117
- # variable has type "ndarray")
1118
- self . _blklocs = None # type: ignore[assignment]
1045
+ for block in blocks :
1046
+ if self .ndim != block . ndim :
1047
+ raise AssertionError (
1048
+ f"Number of Block dimensions ( { block . ndim } ) must equal "
1049
+ f"number of axes ( { self . ndim } )"
1050
+ )
1051
+
1052
+ self . _verify_integrity ()
1119
1053
1120
1054
def _verify_integrity (self ) -> None :
1121
1055
mgr_shape = self .shape
@@ -1130,21 +1064,6 @@ def _verify_integrity(self) -> None:
1130
1064
f"tot_items: { tot_items } "
1131
1065
)
1132
1066
1133
- @classmethod
1134
- def _simple_new (cls , blocks : tuple [Block , ...], axes : list [Index ]):
1135
- """
1136
- Fastpath constructor; does NO validation.
1137
- """
1138
- obj = cls .__new__ (cls )
1139
- obj .axes = axes
1140
- obj .blocks = blocks
1141
-
1142
- # Populate known_consolidate, blknos, and blklocs lazily
1143
- obj ._known_consolidated = False
1144
- obj ._blknos = None
1145
- obj ._blklocs = None
1146
- return obj
1147
-
1148
1067
@classmethod
1149
1068
def from_blocks (cls , blocks : list [Block ], axes : list [Index ]) -> BlockManager :
1150
1069
"""
@@ -1210,7 +1129,7 @@ def get_slice(self, slobj: slice, axis: int = 0) -> BlockManager:
1210
1129
new_axes = list (self .axes )
1211
1130
new_axes [axis ] = new_axes [axis ]._getitem_slice (slobj )
1212
1131
1213
- return type (self ). _simple_new (tuple (new_blocks ), new_axes )
1132
+ return type (self )(tuple (new_blocks ), new_axes , verify_integrity = False )
1214
1133
1215
1134
def iget (self , i : int ) -> SingleBlockManager :
1216
1135
"""
@@ -1418,7 +1337,7 @@ def idelete(self, indexer) -> BlockManager:
1418
1337
nbs = self ._slice_take_blocks_ax0 (taker , only_slice = True )
1419
1338
new_columns = self .items [~ is_deleted ]
1420
1339
axes = [new_columns , self .axes [1 ]]
1421
- return type (self ). _simple_new (tuple (nbs ), axes )
1340
+ return type (self )(tuple (nbs ), axes )
1422
1341
1423
1342
# ----------------------------------------------------------------
1424
1343
# Block-wise Operation
@@ -1602,6 +1521,45 @@ def from_array(cls, array: ArrayLike, index: Index) -> SingleBlockManager:
1602
1521
block = new_block (array , placement = slice (0 , len (index )), ndim = 1 )
1603
1522
return cls (block , index )
1604
1523
1524
+ def __getstate__ (self ):
1525
+ block_values = [b .values for b in self .blocks ]
1526
+ block_items = [self .items [b .mgr_locs .indexer ] for b in self .blocks ]
1527
+ axes_array = list (self .axes )
1528
+
1529
+ extra_state = {
1530
+ "0.14.1" : {
1531
+ "axes" : axes_array ,
1532
+ "blocks" : [
1533
+ {"values" : b .values , "mgr_locs" : b .mgr_locs .indexer }
1534
+ for b in self .blocks
1535
+ ],
1536
+ }
1537
+ }
1538
+
1539
+ # First three elements of the state are to maintain forward
1540
+ # compatibility with 0.13.1.
1541
+ return axes_array , block_values , block_items , extra_state
1542
+
1543
+ def __setstate__ (self , state ):
1544
+ def unpickle_block (values , mgr_locs , ndim : int ) -> Block :
1545
+ # TODO(EA2D): ndim would be unnecessary with 2D EAs
1546
+ # older pickles may store e.g. DatetimeIndex instead of DatetimeArray
1547
+ values = extract_array (values , extract_numpy = True )
1548
+ return new_block (values , placement = mgr_locs , ndim = ndim )
1549
+
1550
+ if isinstance (state , tuple ) and len (state ) >= 4 and "0.14.1" in state [3 ]:
1551
+ state = state [3 ]["0.14.1" ]
1552
+ self .axes = [ensure_index (ax ) for ax in state ["axes" ]]
1553
+ ndim = len (self .axes )
1554
+ self .blocks = tuple (
1555
+ unpickle_block (b ["values" ], b ["mgr_locs" ], ndim = ndim )
1556
+ for b in state ["blocks" ]
1557
+ )
1558
+ else :
1559
+ raise NotImplementedError ("pre-0.14.1 pickles are no longer supported" )
1560
+
1561
+ self ._post_setstate ()
1562
+
1605
1563
def _post_setstate (self ):
1606
1564
pass
1607
1565
0 commit comments