11
11
import pandas ._libs .internals as libinternals
12
12
from pandas ._libs .tslibs import Timedelta , conversion
13
13
from pandas ._libs .tslibs .timezones import tz_compare
14
+ from pandas ._typing import DtypeObj
14
15
from pandas .util ._validators import validate_bool_kwarg
15
16
16
17
from pandas .core .dtypes .cast import (
@@ -170,20 +171,20 @@ def _consolidate_key(self):
170
171
return (self ._can_consolidate , self .dtype .name )
171
172
172
173
@property
173
- def _is_single_block (self ):
174
+ def _is_single_block (self ) -> bool :
174
175
return self .ndim == 1
175
176
176
177
@property
177
- def is_view (self ):
178
+ def is_view (self ) -> bool :
178
179
""" return a boolean if I am possibly a view """
179
180
return self .values .base is not None
180
181
181
182
@property
182
- def is_datelike (self ):
183
+ def is_datelike (self ) -> bool :
183
184
""" return True if I am a non-datelike """
184
185
return self .is_datetime or self .is_timedelta
185
186
186
- def is_categorical_astype (self , dtype ):
187
+ def is_categorical_astype (self , dtype ) -> bool :
187
188
"""
188
189
validate that we have a astypeable to categorical,
189
190
returns a boolean if we are a categorical
@@ -255,7 +256,7 @@ def mgr_locs(self, new_mgr_locs):
255
256
self ._mgr_locs = new_mgr_locs
256
257
257
258
@property
258
- def array_dtype (self ):
259
+ def array_dtype (self ) -> DtypeObj :
259
260
"""
260
261
the dtype to return if I want to construct this block as an
261
262
array
@@ -333,7 +334,7 @@ def dtype(self):
333
334
return self .values .dtype
334
335
335
336
@property
336
- def ftype (self ):
337
+ def ftype (self ) -> str :
337
338
if getattr (self .values , "_pandas_ftype" , False ):
338
339
dtype = self .dtype .subtype
339
340
else :
@@ -367,7 +368,7 @@ def set(self, locs, values):
367
368
"""
368
369
self .values [locs ] = values
369
370
370
- def delete (self , loc ):
371
+ def delete (self , loc ) -> None :
371
372
"""
372
373
Delete given loc(-s) from block in-place.
373
374
"""
@@ -401,7 +402,7 @@ def _split_op_result(self, result) -> List["Block"]:
401
402
402
403
return [result ]
403
404
404
- def fillna (self , value , limit = None , inplace = False , downcast = None ):
405
+ def fillna (self , value , limit = None , inplace : bool = False , downcast = None ):
405
406
"""
406
407
fillna on the block with the value. If we fail, then convert to
407
408
ObjectBlock and try again
@@ -687,15 +688,21 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
687
688
return values
688
689
689
690
# block actions #
690
- def copy (self , deep = True ):
691
+ def copy (self , deep : bool = True ):
691
692
""" copy constructor """
692
693
values = self .values
693
694
if deep :
694
695
values = values .copy ()
695
696
return self .make_block_same_class (values , ndim = self .ndim )
696
697
697
698
def replace (
698
- self , to_replace , value , inplace = False , filter = None , regex = False , convert = True
699
+ self ,
700
+ to_replace ,
701
+ value ,
702
+ inplace : bool = False ,
703
+ filter = None ,
704
+ regex : bool = False ,
705
+ convert : bool = True ,
699
706
):
700
707
"""
701
708
replace the to_replace value with value, possible to create new
@@ -917,7 +924,15 @@ def setitem(self, indexer, value):
917
924
block = self .make_block (values )
918
925
return block
919
926
920
- def putmask (self , mask , new , align = True , inplace = False , axis = 0 , transpose = False ):
927
+ def putmask (
928
+ self ,
929
+ mask ,
930
+ new ,
931
+ align : bool = True ,
932
+ inplace : bool = False ,
933
+ axis : int = 0 ,
934
+ transpose : bool = False ,
935
+ ):
921
936
"""
922
937
putmask the data to the block; it is possible that we may create a
923
938
new dtype of block
@@ -1261,7 +1276,7 @@ def func(x):
1261
1276
blocks = [self .make_block_same_class (interp_values )]
1262
1277
return self ._maybe_downcast (blocks , downcast )
1263
1278
1264
- def take_nd (self , indexer , axis , new_mgr_locs = None , fill_tuple = None ):
1279
+ def take_nd (self , indexer , axis : int , new_mgr_locs = None , fill_tuple = None ):
1265
1280
"""
1266
1281
Take values according to indexer and return them as a block.bb
1267
1282
@@ -1302,7 +1317,7 @@ def diff(self, n: int, axis: int = 1) -> List["Block"]:
1302
1317
new_values = _block_shape (new_values , ndim = self .ndim )
1303
1318
return [self .make_block (values = new_values )]
1304
1319
1305
- def shift (self , periods , axis = 0 , fill_value = None ):
1320
+ def shift (self , periods , axis : int = 0 , fill_value = None ):
1306
1321
""" shift the block by periods, possibly upcast """
1307
1322
# convert integer to float if necessary. need to do a lot more than
1308
1323
# that, handle boolean etc also
@@ -1334,7 +1349,7 @@ def where(
1334
1349
self ,
1335
1350
other ,
1336
1351
cond ,
1337
- align = True ,
1352
+ align : bool = True ,
1338
1353
errors = "raise" ,
1339
1354
try_cast : bool = False ,
1340
1355
axis : int = 0 ,
@@ -1346,11 +1361,12 @@ def where(
1346
1361
----------
1347
1362
other : a ndarray/object
1348
1363
cond : the condition to respect
1349
- align : boolean, perform alignment on other/cond
1364
+ align : bool, default True
1365
+ Perform alignment on other/cond.
1350
1366
errors : str, {'raise', 'ignore'}, default 'raise'
1351
1367
- ``raise`` : allow exceptions to be raised
1352
1368
- ``ignore`` : suppress exceptions. On error return original object
1353
- axis : int
1369
+ axis : int, default 0
1354
1370
1355
1371
Returns
1356
1372
-------
@@ -1482,7 +1498,7 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
1482
1498
blocks = [make_block (new_values , placement = new_placement )]
1483
1499
return blocks , mask
1484
1500
1485
- def quantile (self , qs , interpolation = "linear" , axis = 0 ):
1501
+ def quantile (self , qs , interpolation = "linear" , axis : int = 0 ):
1486
1502
"""
1487
1503
compute the quantiles of the
1488
1504
@@ -1539,7 +1555,13 @@ def quantile(self, qs, interpolation="linear", axis=0):
1539
1555
return make_block (result , placement = np .arange (len (result )), ndim = ndim )
1540
1556
1541
1557
def _replace_coerce (
1542
- self , to_replace , value , inplace = True , regex = False , convert = False , mask = None
1558
+ self ,
1559
+ to_replace ,
1560
+ value ,
1561
+ inplace : bool = True ,
1562
+ regex : bool = False ,
1563
+ convert : bool = False ,
1564
+ mask = None ,
1543
1565
):
1544
1566
"""
1545
1567
Replace value corresponding to the given boolean array with another
@@ -1551,7 +1573,7 @@ def _replace_coerce(
1551
1573
Scalar to replace or regular expression to match.
1552
1574
value : object
1553
1575
Replacement object.
1554
- inplace : bool, default False
1576
+ inplace : bool, default True
1555
1577
Perform inplace modification.
1556
1578
regex : bool, default False
1557
1579
If true, perform regular expression substitution.
@@ -1638,7 +1660,9 @@ def set(self, locs, values, check=False):
1638
1660
assert locs .tolist () == [0 ]
1639
1661
self .values = values
1640
1662
1641
- def putmask (self , mask , new , align = True , inplace = False , axis = 0 , transpose = False ):
1663
+ def putmask (
1664
+ self , mask , new , align = True , inplace = False , axis = 0 , transpose = False ,
1665
+ ):
1642
1666
"""
1643
1667
putmask the data to the block; we must be a single block and not
1644
1668
generate other blocks
@@ -1754,7 +1778,7 @@ def _can_hold_na(self):
1754
1778
return self ._holder ._can_hold_na
1755
1779
1756
1780
@property
1757
- def is_view (self ):
1781
+ def is_view (self ) -> bool :
1758
1782
"""Extension arrays are never treated as views."""
1759
1783
return False
1760
1784
@@ -1819,7 +1843,7 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
1819
1843
# we are expected to return a 2-d ndarray
1820
1844
return values .reshape (1 , len (values ))
1821
1845
1822
- def take_nd (self , indexer , axis = 0 , new_mgr_locs = None , fill_tuple = None ):
1846
+ def take_nd (self , indexer , axis : int = 0 , new_mgr_locs = None , fill_tuple = None ):
1823
1847
"""
1824
1848
Take values according to indexer and return them as a block.
1825
1849
"""
@@ -2080,7 +2104,7 @@ def to_native_types(
2080
2104
)
2081
2105
return formatter .get_result_as_array ()
2082
2106
2083
- def should_store (self , value ):
2107
+ def should_store (self , value ) -> bool :
2084
2108
# when inserting a column should not coerce integers to floats
2085
2109
# unnecessarily
2086
2110
return issubclass (value .dtype .type , np .floating ) and value .dtype == self .dtype
@@ -2098,7 +2122,7 @@ def _can_hold_element(self, element: Any) -> bool:
2098
2122
element , (float , int , complex , np .float_ , np .int_ )
2099
2123
) and not isinstance (element , (bool , np .bool_ ))
2100
2124
2101
- def should_store (self , value ):
2125
+ def should_store (self , value ) -> bool :
2102
2126
return issubclass (value .dtype .type , np .complexfloating )
2103
2127
2104
2128
@@ -2117,7 +2141,7 @@ def _can_hold_element(self, element: Any) -> bool:
2117
2141
)
2118
2142
return is_integer (element )
2119
2143
2120
- def should_store (self , value ):
2144
+ def should_store (self , value ) -> bool :
2121
2145
return is_integer_dtype (value ) and value .dtype == self .dtype
2122
2146
2123
2147
@@ -2255,7 +2279,7 @@ def to_native_types(
2255
2279
).reshape (i8values .shape )
2256
2280
return np .atleast_2d (result )
2257
2281
2258
- def should_store (self , value ):
2282
+ def should_store (self , value ) -> bool :
2259
2283
return (
2260
2284
issubclass (value .dtype .type , np .datetime64 )
2261
2285
and not is_datetime64tz_dtype (value )
@@ -2320,7 +2344,7 @@ def _maybe_coerce_values(self, values):
2320
2344
return values
2321
2345
2322
2346
@property
2323
- def is_view (self ):
2347
+ def is_view (self ) -> bool :
2324
2348
""" return a boolean if I am possibly a view """
2325
2349
# check the ndarray values of the DatetimeIndex values
2326
2350
return self .values ._data .base is not None
@@ -2507,7 +2531,7 @@ def fillna(self, value, **kwargs):
2507
2531
)
2508
2532
return super ().fillna (value , ** kwargs )
2509
2533
2510
- def should_store (self , value ):
2534
+ def should_store (self , value ) -> bool :
2511
2535
return issubclass (
2512
2536
value .dtype .type , np .timedelta64
2513
2537
) and not is_extension_array_dtype (value )
@@ -2553,7 +2577,7 @@ def _can_hold_element(self, element: Any) -> bool:
2553
2577
return issubclass (tipo .type , np .bool_ )
2554
2578
return isinstance (element , (bool , np .bool_ ))
2555
2579
2556
- def should_store (self , value ):
2580
+ def should_store (self , value ) -> bool :
2557
2581
return issubclass (value .dtype .type , np .bool_ ) and not is_extension_array_dtype (
2558
2582
value
2559
2583
)
@@ -2645,7 +2669,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
2645
2669
def _can_hold_element (self , element : Any ) -> bool :
2646
2670
return True
2647
2671
2648
- def should_store (self , value ):
2672
+ def should_store (self , value ) -> bool :
2649
2673
return not (
2650
2674
issubclass (
2651
2675
value .dtype .type ,
0 commit comments