Skip to content

Commit 7abac27

Browse files
authored
TYP: blocks (#39543)
1 parent b06bd2d commit 7abac27

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

pandas/core/internals/blocks.py

+36-28
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def array_values(self) -> ExtensionArray:
239239
"""
240240
return PandasArray(self.values)
241241

242-
def get_values(self, dtype: Optional[Dtype] = None):
242+
def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
243243
"""
244244
return an internal format, currently just the ndarray
245245
this is often overridden to handle to_dense like operations
@@ -282,7 +282,7 @@ def make_block(self, values, placement=None) -> Block:
282282

283283
return make_block(values, placement=placement, ndim=self.ndim)
284284

285-
def make_block_same_class(self, values, placement=None, ndim=None):
285+
def make_block_same_class(self, values, placement=None, ndim=None) -> Block:
286286
""" Wrap given values in a block of same type as self. """
287287
if placement is None:
288288
placement = self.mgr_locs
@@ -318,7 +318,7 @@ def _slice(self, slicer):
318318

319319
return self.values[slicer]
320320

321-
def getitem_block(self, slicer, new_mgr_locs=None):
321+
def getitem_block(self, slicer, new_mgr_locs=None) -> Block:
322322
"""
323323
Perform __getitem__-like, return result as block.
324324
@@ -338,11 +338,11 @@ def getitem_block(self, slicer, new_mgr_locs=None):
338338
return type(self)._simple_new(new_values, new_mgr_locs, self.ndim)
339339

340340
@property
341-
def shape(self):
341+
def shape(self) -> Shape:
342342
return self.values.shape
343343

344344
@property
345-
def dtype(self):
345+
def dtype(self) -> DtypeObj:
346346
return self.values.dtype
347347

348348
def iget(self, i):
@@ -1063,7 +1063,7 @@ def f(mask, val, idx):
10631063
new_blocks = self.split_and_operate(mask, f, True)
10641064
return new_blocks
10651065

1066-
def coerce_to_target_dtype(self, other):
1066+
def coerce_to_target_dtype(self, other) -> Block:
10671067
"""
10681068
coerce the current block to a dtype compat for other
10691069
we will return a block, possibly object, and not raise
@@ -1091,13 +1091,13 @@ def interpolate(
10911091
coerce: bool = False,
10921092
downcast: Optional[str] = None,
10931093
**kwargs,
1094-
):
1094+
) -> List[Block]:
10951095

10961096
inplace = validate_bool_kwarg(inplace, "inplace")
10971097

10981098
if not self._can_hold_na:
10991099
# If there are no NAs, then interpolate is a no-op
1100-
return self if inplace else self.copy()
1100+
return [self] if inplace else [self.copy()]
11011101

11021102
# a fill na type method
11031103
try:
@@ -1219,7 +1219,9 @@ def func(yvalues: np.ndarray) -> np.ndarray:
12191219
blocks = [self.make_block_same_class(interp_values)]
12201220
return self._maybe_downcast(blocks, downcast)
12211221

1222-
def take_nd(self, indexer, axis: int, new_mgr_locs=None, fill_value=lib.no_default):
1222+
def take_nd(
1223+
self, indexer, axis: int, new_mgr_locs=None, fill_value=lib.no_default
1224+
) -> Block:
12231225
"""
12241226
Take values according to indexer and return them as a block.bb
12251227
@@ -1256,7 +1258,7 @@ def diff(self, n: int, axis: int = 1) -> List[Block]:
12561258
new_values = algos.diff(self.values, n, axis=axis, stacklevel=7)
12571259
return [self.make_block(values=new_values)]
12581260

1259-
def shift(self, periods: int, axis: int = 0, fill_value=None):
1261+
def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Block]:
12601262
""" shift the block by periods, possibly upcast """
12611263
# convert integer to float if necessary. need to do a lot more than
12621264
# that, handle boolean etc also
@@ -1369,7 +1371,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
13691371
blocks = [make_block(new_values, placement=new_placement)]
13701372
return blocks, mask
13711373

1372-
def quantile(self, qs, interpolation="linear", axis: int = 0):
1374+
def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
13731375
"""
13741376
compute the quantiles of the
13751377
@@ -1521,7 +1523,7 @@ def __init__(self, values, placement, ndim: int):
15211523
raise AssertionError("block.size != values.size")
15221524

15231525
@property
1524-
def shape(self):
1526+
def shape(self) -> Shape:
15251527
# TODO(EA2D): override unnecessary with 2D EAs
15261528
if self.ndim == 1:
15271529
return (len(self.values),)
@@ -1647,7 +1649,7 @@ def setitem(self, indexer, value):
16471649
self.values[indexer] = value
16481650
return self
16491651

1650-
def get_values(self, dtype: Optional[Dtype] = None):
1652+
def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
16511653
# ExtensionArrays must be iterable, so this works.
16521654
# TODO(EA2D): reshape not needed with 2D EAs
16531655
return np.asarray(self.values).reshape(self.shape)
@@ -1669,7 +1671,7 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
16691671

16701672
def take_nd(
16711673
self, indexer, axis: int = 0, new_mgr_locs=None, fill_value=lib.no_default
1672-
):
1674+
) -> Block:
16731675
"""
16741676
Take values according to indexer and return them as a block.
16751677
"""
@@ -1733,7 +1735,9 @@ def _slice(self, slicer):
17331735

17341736
return self.values[slicer]
17351737

1736-
def fillna(self, value, limit=None, inplace=False, downcast=None):
1738+
def fillna(
1739+
self, value, limit=None, inplace: bool = False, downcast=None
1740+
) -> List[Block]:
17371741
values = self.values if inplace else self.values.copy()
17381742
values = values.fillna(value=value, limit=limit)
17391743
return [
@@ -1765,9 +1769,7 @@ def diff(self, n: int, axis: int = 1) -> List[Block]:
17651769
axis = 0
17661770
return super().diff(n, axis)
17671771

1768-
def shift(
1769-
self, periods: int, axis: int = 0, fill_value: Any = None
1770-
) -> List[ExtensionBlock]:
1772+
def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Block]:
17711773
"""
17721774
Shift the block by `periods`.
17731775
@@ -1947,7 +1949,7 @@ def _holder(self):
19471949
def fill_value(self):
19481950
return np.datetime64("NaT", "ns")
19491951

1950-
def get_values(self, dtype: Optional[Dtype] = None):
1952+
def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
19511953
"""
19521954
return object dtype as boxed values, such as Timestamps/Timedelta
19531955
"""
@@ -1996,11 +1998,11 @@ def diff(self, n: int, axis: int = 0) -> List[Block]:
19961998
TimeDeltaBlock(new_values, placement=self.mgr_locs.indexer, ndim=self.ndim)
19971999
]
19982000

1999-
def shift(self, periods, axis=0, fill_value=None):
2001+
def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Block]:
20002002
# TODO(EA2D) this is unnecessary if these blocks are backed by 2D EAs
20012003
values = self.array_values()
20022004
new_values = values.shift(periods, fill_value=fill_value, axis=axis)
2003-
return self.make_block_same_class(new_values)
2005+
return [self.make_block_same_class(new_values)]
20042006

20052007
def to_native_types(self, na_rep="NaT", **kwargs):
20062008
""" convert to our native types format """
@@ -2118,7 +2120,7 @@ def is_view(self) -> bool:
21182120
# check the ndarray values of the DatetimeIndex values
21192121
return self.values._data.base is not None
21202122

2121-
def get_values(self, dtype: Optional[Dtype] = None):
2123+
def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray:
21222124
"""
21232125
Returns an ndarray of values.
21242126
@@ -2157,7 +2159,9 @@ def external_values(self):
21572159
return self.values._data
21582160
return np.asarray(self.values.astype("datetime64[ns]", copy=False))
21592161

2160-
def fillna(self, value, limit=None, inplace=False, downcast=None):
2162+
def fillna(
2163+
self, value, limit=None, inplace: bool = False, downcast=None
2164+
) -> List[Block]:
21612165
# We support filling a DatetimeTZ with a `value` whose timezone
21622166
# is different by coercing to object.
21632167
if self._can_hold_element(value):
@@ -2168,7 +2172,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
21682172
value, limit=limit, inplace=inplace, downcast=downcast
21692173
)
21702174

2171-
def quantile(self, qs, interpolation="linear", axis=0):
2175+
def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
21722176
naive = self.values.view("M8[ns]")
21732177

21742178
# TODO(EA2D): kludge for 2D block with 1D values
@@ -2228,7 +2232,9 @@ def _maybe_coerce_values(self, values):
22282232
def _holder(self):
22292233
return TimedeltaArray
22302234

2231-
def fillna(self, value, **kwargs):
2235+
def fillna(
2236+
self, value, limit=None, inplace: bool = False, downcast=None
2237+
) -> List[Block]:
22322238
# TODO(EA2D): if we operated on array_values, TDA.fillna would handle
22332239
# raising here.
22342240
if is_integer(value):
@@ -2238,7 +2244,7 @@ def fillna(self, value, **kwargs):
22382244
"longer supported. To obtain the old behavior, pass "
22392245
"`pd.Timedelta(seconds=n)` instead."
22402246
)
2241-
return super().fillna(value, **kwargs)
2247+
return super().fillna(value, limit=limit, inplace=inplace, downcast=downcast)
22422248

22432249

22442250
class ObjectBlock(Block):
@@ -2450,7 +2456,9 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
24502456
return cls
24512457

24522458

2453-
def make_block(values, placement, klass=None, ndim=None, dtype: Optional[Dtype] = None):
2459+
def make_block(
2460+
values, placement, klass=None, ndim=None, dtype: Optional[Dtype] = None
2461+
) -> Block:
24542462
# Ensure that we don't allow PandasArray / PandasDtype in internals.
24552463
# For now, blocks should be backed by ndarrays when possible.
24562464
if isinstance(values, ABCPandasArray):
@@ -2477,7 +2485,7 @@ def make_block(values, placement, klass=None, ndim=None, dtype: Optional[Dtype]
24772485
# -----------------------------------------------------------------
24782486

24792487

2480-
def extend_blocks(result, blocks=None):
2488+
def extend_blocks(result, blocks=None) -> List[Block]:
24812489
""" return a new extended blocks, given the result """
24822490
if blocks is None:
24832491
blocks = []

0 commit comments

Comments
 (0)