Skip to content

Commit 283f81c

Browse files
authored
TYP: internals (#32403)
1 parent 0dc93cd commit 283f81c

File tree

2 files changed

+114
-71
lines changed

2 files changed

+114
-71
lines changed

pandas/core/internals/blocks.py

+54-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pandas._libs.internals as libinternals
1212
from pandas._libs.tslibs import Timedelta, conversion
1313
from pandas._libs.tslibs.timezones import tz_compare
14+
from pandas._typing import DtypeObj
1415
from pandas.util._validators import validate_bool_kwarg
1516

1617
from pandas.core.dtypes.cast import (
@@ -170,20 +171,20 @@ def _consolidate_key(self):
170171
return (self._can_consolidate, self.dtype.name)
171172

172173
@property
173-
def _is_single_block(self):
174+
def _is_single_block(self) -> bool:
174175
return self.ndim == 1
175176

176177
@property
177-
def is_view(self):
178+
def is_view(self) -> bool:
178179
""" return a boolean if I am possibly a view """
179180
return self.values.base is not None
180181

181182
@property
182-
def is_datelike(self):
183+
def is_datelike(self) -> bool:
183184
""" return True if I am a non-datelike """
184185
return self.is_datetime or self.is_timedelta
185186

186-
def is_categorical_astype(self, dtype):
187+
def is_categorical_astype(self, dtype) -> bool:
187188
"""
188189
validate that we have a astypeable to categorical,
189190
returns a boolean if we are a categorical
@@ -255,7 +256,7 @@ def mgr_locs(self, new_mgr_locs):
255256
self._mgr_locs = new_mgr_locs
256257

257258
@property
258-
def array_dtype(self):
259+
def array_dtype(self) -> DtypeObj:
259260
"""
260261
the dtype to return if I want to construct this block as an
261262
array
@@ -333,7 +334,7 @@ def dtype(self):
333334
return self.values.dtype
334335

335336
@property
336-
def ftype(self):
337+
def ftype(self) -> str:
337338
if getattr(self.values, "_pandas_ftype", False):
338339
dtype = self.dtype.subtype
339340
else:
@@ -367,7 +368,7 @@ def set(self, locs, values):
367368
"""
368369
self.values[locs] = values
369370

370-
def delete(self, loc):
371+
def delete(self, loc) -> None:
371372
"""
372373
Delete given loc(-s) from block in-place.
373374
"""
@@ -401,7 +402,7 @@ def _split_op_result(self, result) -> List["Block"]:
401402

402403
return [result]
403404

404-
def fillna(self, value, limit=None, inplace=False, downcast=None):
405+
def fillna(self, value, limit=None, inplace: bool = False, downcast=None):
405406
"""
406407
fillna on the block with the value. If we fail, then convert to
407408
ObjectBlock and try again
@@ -687,15 +688,21 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
687688
return values
688689

689690
# block actions #
690-
def copy(self, deep=True):
691+
def copy(self, deep: bool = True):
691692
""" copy constructor """
692693
values = self.values
693694
if deep:
694695
values = values.copy()
695696
return self.make_block_same_class(values, ndim=self.ndim)
696697

697698
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,
699706
):
700707
"""
701708
replace the to_replace value with value, possible to create new
@@ -917,7 +924,15 @@ def setitem(self, indexer, value):
917924
block = self.make_block(values)
918925
return block
919926

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+
):
921936
"""
922937
putmask the data to the block; it is possible that we may create a
923938
new dtype of block
@@ -1261,7 +1276,7 @@ def func(x):
12611276
blocks = [self.make_block_same_class(interp_values)]
12621277
return self._maybe_downcast(blocks, downcast)
12631278

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):
12651280
"""
12661281
Take values according to indexer and return them as a block.bb
12671282
@@ -1302,7 +1317,7 @@ def diff(self, n: int, axis: int = 1) -> List["Block"]:
13021317
new_values = _block_shape(new_values, ndim=self.ndim)
13031318
return [self.make_block(values=new_values)]
13041319

1305-
def shift(self, periods, axis=0, fill_value=None):
1320+
def shift(self, periods, axis: int = 0, fill_value=None):
13061321
""" shift the block by periods, possibly upcast """
13071322
# convert integer to float if necessary. need to do a lot more than
13081323
# that, handle boolean etc also
@@ -1334,7 +1349,7 @@ def where(
13341349
self,
13351350
other,
13361351
cond,
1337-
align=True,
1352+
align: bool = True,
13381353
errors="raise",
13391354
try_cast: bool = False,
13401355
axis: int = 0,
@@ -1346,11 +1361,12 @@ def where(
13461361
----------
13471362
other : a ndarray/object
13481363
cond : the condition to respect
1349-
align : boolean, perform alignment on other/cond
1364+
align : bool, default True
1365+
Perform alignment on other/cond.
13501366
errors : str, {'raise', 'ignore'}, default 'raise'
13511367
- ``raise`` : allow exceptions to be raised
13521368
- ``ignore`` : suppress exceptions. On error return original object
1353-
axis : int
1369+
axis : int, default 0
13541370
13551371
Returns
13561372
-------
@@ -1482,7 +1498,7 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
14821498
blocks = [make_block(new_values, placement=new_placement)]
14831499
return blocks, mask
14841500

1485-
def quantile(self, qs, interpolation="linear", axis=0):
1501+
def quantile(self, qs, interpolation="linear", axis: int = 0):
14861502
"""
14871503
compute the quantiles of the
14881504
@@ -1539,7 +1555,13 @@ def quantile(self, qs, interpolation="linear", axis=0):
15391555
return make_block(result, placement=np.arange(len(result)), ndim=ndim)
15401556

15411557
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,
15431565
):
15441566
"""
15451567
Replace value corresponding to the given boolean array with another
@@ -1551,7 +1573,7 @@ def _replace_coerce(
15511573
Scalar to replace or regular expression to match.
15521574
value : object
15531575
Replacement object.
1554-
inplace : bool, default False
1576+
inplace : bool, default True
15551577
Perform inplace modification.
15561578
regex : bool, default False
15571579
If true, perform regular expression substitution.
@@ -1638,7 +1660,9 @@ def set(self, locs, values, check=False):
16381660
assert locs.tolist() == [0]
16391661
self.values = values
16401662

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+
):
16421666
"""
16431667
putmask the data to the block; we must be a single block and not
16441668
generate other blocks
@@ -1754,7 +1778,7 @@ def _can_hold_na(self):
17541778
return self._holder._can_hold_na
17551779

17561780
@property
1757-
def is_view(self):
1781+
def is_view(self) -> bool:
17581782
"""Extension arrays are never treated as views."""
17591783
return False
17601784

@@ -1819,7 +1843,7 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
18191843
# we are expected to return a 2-d ndarray
18201844
return values.reshape(1, len(values))
18211845

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):
18231847
"""
18241848
Take values according to indexer and return them as a block.
18251849
"""
@@ -2080,7 +2104,7 @@ def to_native_types(
20802104
)
20812105
return formatter.get_result_as_array()
20822106

2083-
def should_store(self, value):
2107+
def should_store(self, value) -> bool:
20842108
# when inserting a column should not coerce integers to floats
20852109
# unnecessarily
20862110
return issubclass(value.dtype.type, np.floating) and value.dtype == self.dtype
@@ -2098,7 +2122,7 @@ def _can_hold_element(self, element: Any) -> bool:
20982122
element, (float, int, complex, np.float_, np.int_)
20992123
) and not isinstance(element, (bool, np.bool_))
21002124

2101-
def should_store(self, value):
2125+
def should_store(self, value) -> bool:
21022126
return issubclass(value.dtype.type, np.complexfloating)
21032127

21042128

@@ -2117,7 +2141,7 @@ def _can_hold_element(self, element: Any) -> bool:
21172141
)
21182142
return is_integer(element)
21192143

2120-
def should_store(self, value):
2144+
def should_store(self, value) -> bool:
21212145
return is_integer_dtype(value) and value.dtype == self.dtype
21222146

21232147

@@ -2255,7 +2279,7 @@ def to_native_types(
22552279
).reshape(i8values.shape)
22562280
return np.atleast_2d(result)
22572281

2258-
def should_store(self, value):
2282+
def should_store(self, value) -> bool:
22592283
return (
22602284
issubclass(value.dtype.type, np.datetime64)
22612285
and not is_datetime64tz_dtype(value)
@@ -2320,7 +2344,7 @@ def _maybe_coerce_values(self, values):
23202344
return values
23212345

23222346
@property
2323-
def is_view(self):
2347+
def is_view(self) -> bool:
23242348
""" return a boolean if I am possibly a view """
23252349
# check the ndarray values of the DatetimeIndex values
23262350
return self.values._data.base is not None
@@ -2507,7 +2531,7 @@ def fillna(self, value, **kwargs):
25072531
)
25082532
return super().fillna(value, **kwargs)
25092533

2510-
def should_store(self, value):
2534+
def should_store(self, value) -> bool:
25112535
return issubclass(
25122536
value.dtype.type, np.timedelta64
25132537
) and not is_extension_array_dtype(value)
@@ -2553,7 +2577,7 @@ def _can_hold_element(self, element: Any) -> bool:
25532577
return issubclass(tipo.type, np.bool_)
25542578
return isinstance(element, (bool, np.bool_))
25552579

2556-
def should_store(self, value):
2580+
def should_store(self, value) -> bool:
25572581
return issubclass(value.dtype.type, np.bool_) and not is_extension_array_dtype(
25582582
value
25592583
)
@@ -2645,7 +2669,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
26452669
def _can_hold_element(self, element: Any) -> bool:
26462670
return True
26472671

2648-
def should_store(self, value):
2672+
def should_store(self, value) -> bool:
26492673
return not (
26502674
issubclass(
26512675
value.dtype.type,

0 commit comments

Comments
 (0)