32
32
Dtype ,
33
33
DtypeObj ,
34
34
Shape ,
35
+ final ,
35
36
)
36
37
from pandas .util ._validators import validate_bool_kwarg
37
38
@@ -231,6 +232,7 @@ def _holder(self):
231
232
"""
232
233
return None
233
234
235
+ @final
234
236
@property
235
237
def _consolidate_key (self ):
236
238
return self ._can_consolidate , self .dtype .name
@@ -242,6 +244,7 @@ def is_view(self) -> bool:
242
244
values = cast (np .ndarray , values )
243
245
return values .base is not None
244
246
247
+ @final
245
248
@property
246
249
def is_categorical (self ) -> bool :
247
250
return self ._holder is Categorical
@@ -278,6 +281,7 @@ def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
278
281
return self .values .astype (object )
279
282
return self .values
280
283
284
+ @final
281
285
def get_block_values_for_json (self ) -> np .ndarray :
282
286
"""
283
287
This is used in the JSON C code.
@@ -300,6 +304,7 @@ def mgr_locs(self, new_mgr_locs):
300
304
301
305
self ._mgr_locs = new_mgr_locs
302
306
307
+ @final
303
308
def make_block (self , values , placement = None ) -> Block :
304
309
"""
305
310
Create a new block, with type inference propagate any values that are
@@ -312,14 +317,14 @@ def make_block(self, values, placement=None) -> Block:
312
317
313
318
return make_block (values , placement = placement , ndim = self .ndim )
314
319
315
- def make_block_same_class (self , values , placement = None , ndim = None ) -> Block :
320
+ @final
321
+ def make_block_same_class (self , values , placement = None ) -> Block :
316
322
""" Wrap given values in a block of same type as self. """
317
323
if placement is None :
318
324
placement = self .mgr_locs
319
- if ndim is None :
320
- ndim = self .ndim
321
- return type (self )(values , placement = placement , ndim = ndim )
325
+ return type (self )(values , placement = placement , ndim = self .ndim )
322
326
327
+ @final
323
328
def __repr__ (self ) -> str :
324
329
# don't want to print out all of the items here
325
330
name = type (self ).__name__
@@ -332,12 +337,15 @@ def __repr__(self) -> str:
332
337
333
338
return result
334
339
340
+ @final
335
341
def __len__ (self ) -> int :
336
342
return len (self .values )
337
343
344
+ @final
338
345
def __getstate__ (self ):
339
346
return self .mgr_locs .indexer , self .values
340
347
348
+ @final
341
349
def __setstate__ (self , state ):
342
350
self .mgr_locs = libinternals .BlockPlacement (state [0 ])
343
351
self .values = state [1 ]
@@ -348,6 +356,7 @@ def _slice(self, slicer):
348
356
349
357
return self .values [slicer ]
350
358
359
+ @final
351
360
def getitem_block (self , slicer , new_mgr_locs = None ) -> Block :
352
361
"""
353
362
Perform __getitem__-like, return result as block.
@@ -371,6 +380,7 @@ def getitem_block(self, slicer, new_mgr_locs=None) -> Block:
371
380
def shape (self ) -> Shape :
372
381
return self .values .shape
373
382
383
+ @final
374
384
@property
375
385
def dtype (self ) -> DtypeObj :
376
386
return self .values .dtype
@@ -389,13 +399,15 @@ def set_inplace(self, locs, values):
389
399
"""
390
400
self .values [locs ] = values
391
401
402
+ @final
392
403
def delete (self , loc ) -> None :
393
404
"""
394
405
Delete given loc(-s) from block in-place.
395
406
"""
396
407
self .values = np .delete (self .values , loc , 0 )
397
408
self .mgr_locs = self .mgr_locs .delete (loc )
398
409
410
+ @final
399
411
def apply (self , func , ** kwargs ) -> List [Block ]:
400
412
"""
401
413
apply the function to my values; return a block if we are not
@@ -427,6 +439,7 @@ def reduce(self, func, ignore_failures: bool = False) -> List[Block]:
427
439
nb = self .make_block (res_values )
428
440
return [nb ]
429
441
442
+ @final
430
443
def _split_op_result (self , result ) -> List [Block ]:
431
444
# See also: split_and_operate
432
445
if is_extension_array_dtype (result ) and result .ndim > 1 :
@@ -487,6 +500,7 @@ def f(mask, val, idx):
487
500
488
501
return self .split_and_operate (None , f , inplace )
489
502
503
+ @final
490
504
def _split (self ) -> List [Block ]:
491
505
"""
492
506
Split a block into a list of single-column blocks.
@@ -501,6 +515,7 @@ def _split(self) -> List[Block]:
501
515
new_blocks .append (nb )
502
516
return new_blocks
503
517
518
+ @final
504
519
def split_and_operate (
505
520
self , mask , f , inplace : bool , ignore_failures : bool = False
506
521
) -> List [Block ]:
@@ -617,6 +632,7 @@ def f(mask, val, idx):
617
632
618
633
return self .split_and_operate (None , f , False )
619
634
635
+ @final
620
636
def astype (self , dtype , copy : bool = False , errors : str = "raise" ):
621
637
"""
622
638
Coerce to the new dtype.
@@ -720,6 +736,7 @@ def _can_hold_element(self, element: Any) -> bool:
720
736
""" require the same dtype as ourselves """
721
737
raise NotImplementedError ("Implemented on subclasses" )
722
738
739
+ @final
723
740
def should_store (self , value : ArrayLike ) -> bool :
724
741
"""
725
742
Should we set self.values[indexer] = value inplace or do we need to cast?
@@ -753,12 +770,13 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
753
770
return self .make_block (values )
754
771
755
772
# block actions #
773
+ @final
756
774
def copy (self , deep : bool = True ):
757
775
""" copy constructor """
758
776
values = self .values
759
777
if deep :
760
778
values = values .copy ()
761
- return self .make_block_same_class (values , ndim = self . ndim )
779
+ return self .make_block_same_class (values )
762
780
763
781
# ---------------------------------------------------------------------
764
782
# Replace
@@ -807,6 +825,7 @@ def replace(
807
825
blocks = blk .convert (numeric = False , copy = not inplace )
808
826
return blocks
809
827
828
+ @final
810
829
def _replace_regex (
811
830
self ,
812
831
to_replace ,
@@ -852,6 +871,7 @@ def _replace_regex(
852
871
nbs = [block ]
853
872
return nbs
854
873
874
+ @final
855
875
def _replace_list (
856
876
self ,
857
877
src_list : List [Any ],
@@ -913,6 +933,7 @@ def _replace_list(
913
933
rb = new_rb
914
934
return rb
915
935
936
+ @final
916
937
def _replace_coerce (
917
938
self ,
918
939
to_replace ,
@@ -1147,6 +1168,7 @@ def f(mask, val, idx):
1147
1168
new_blocks = self .split_and_operate (mask , f , True )
1148
1169
return new_blocks
1149
1170
1171
+ @final
1150
1172
def coerce_to_target_dtype (self , other ) -> Block :
1151
1173
"""
1152
1174
coerce the current block to a dtype compat for other
@@ -1162,6 +1184,7 @@ def coerce_to_target_dtype(self, other) -> Block:
1162
1184
1163
1185
return self .astype (new_dtype , copy = False )
1164
1186
1187
+ @final
1165
1188
def interpolate (
1166
1189
self ,
1167
1190
method : str = "pad" ,
@@ -1220,6 +1243,7 @@ def interpolate(
1220
1243
** kwargs ,
1221
1244
)
1222
1245
1246
+ @final
1223
1247
def _interpolate_with_fill (
1224
1248
self ,
1225
1249
method : str = "pad" ,
@@ -1244,9 +1268,10 @@ def _interpolate_with_fill(
1244
1268
limit_area = limit_area ,
1245
1269
)
1246
1270
1247
- blocks = [self .make_block_same_class (values , ndim = self . ndim )]
1271
+ blocks = [self .make_block_same_class (values )]
1248
1272
return self ._maybe_downcast (blocks , downcast )
1249
1273
1274
+ @final
1250
1275
def _interpolate (
1251
1276
self ,
1252
1277
method : str ,
@@ -1459,7 +1484,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
1459
1484
new_values = new_values .T [mask ]
1460
1485
new_placement = new_placement [mask ]
1461
1486
1462
- blocks = [make_block (new_values , placement = new_placement )]
1487
+ blocks = [make_block (new_values , placement = new_placement , ndim = 2 )]
1463
1488
return blocks , mask
1464
1489
1465
1490
def quantile (
@@ -1761,21 +1786,15 @@ def fillna(
1761
1786
) -> List [Block ]:
1762
1787
values = self .values if inplace else self .values .copy ()
1763
1788
values = values .fillna (value = value , limit = limit )
1764
- return [
1765
- self .make_block_same_class (
1766
- values = values , placement = self .mgr_locs , ndim = self .ndim
1767
- )
1768
- ]
1789
+ return [self .make_block_same_class (values = values )]
1769
1790
1770
1791
def interpolate (
1771
1792
self , method = "pad" , axis = 0 , inplace = False , limit = None , fill_value = None , ** kwargs
1772
1793
):
1773
1794
1774
1795
values = self .values if inplace else self .values .copy ()
1775
- return self .make_block_same_class (
1776
- values = values .fillna (value = fill_value , method = method , limit = limit ),
1777
- placement = self .mgr_locs ,
1778
- )
1796
+ new_values = values .fillna (value = fill_value , method = method , limit = limit )
1797
+ return self .make_block_same_class (new_values )
1779
1798
1780
1799
def diff (self , n : int , axis : int = 1 ) -> List [Block ]:
1781
1800
if axis == 0 and n != 0 :
@@ -1797,13 +1816,8 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
1797
1816
Dispatches to underlying ExtensionArray and re-boxes in an
1798
1817
ExtensionBlock.
1799
1818
"""
1800
- return [
1801
- self .make_block_same_class (
1802
- self .values .shift (periods = periods , fill_value = fill_value ),
1803
- placement = self .mgr_locs ,
1804
- ndim = self .ndim ,
1805
- )
1806
- ]
1819
+ new_values = self .values .shift (periods = periods , fill_value = fill_value )
1820
+ return [self .make_block_same_class (new_values )]
1807
1821
1808
1822
def where (self , other , cond , errors = "raise" , axis : int = 0 ) -> List [Block ]:
1809
1823
@@ -1850,7 +1864,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
1850
1864
np .where (cond , self .values , other ), dtype = dtype
1851
1865
)
1852
1866
1853
- return [self .make_block_same_class (result , placement = self . mgr_locs )]
1867
+ return [self .make_block_same_class (result )]
1854
1868
1855
1869
def _unstack (self , unstacker , fill_value , new_placement ):
1856
1870
# ExtensionArray-safe unstack.
0 commit comments