Skip to content

Commit 39a0534

Browse files
authored
TYP: consistent return types blocks (#36967)
1 parent 41ec93a commit 39a0534

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

pandas/core/internals/blocks.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,11 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
491491

492492
return extend_blocks([b.downcast(downcast) for b in blocks])
493493

494-
def downcast(self, dtypes=None):
494+
def downcast(self, dtypes=None) -> List["Block"]:
495495
""" try to downcast each item to the dict of dtypes if present """
496496
# turn it off completely
497497
if dtypes is False:
498-
return self
498+
return [self]
499499

500500
values = self.values
501501

@@ -506,11 +506,11 @@ def downcast(self, dtypes=None):
506506
dtypes = "infer"
507507

508508
nv = maybe_downcast_to_dtype(values, dtypes)
509-
return self.make_block(nv)
509+
return [self.make_block(nv)]
510510

511511
# ndim > 1
512512
if dtypes is None:
513-
return self
513+
return [self]
514514

515515
if not (dtypes == "infer" or isinstance(dtypes, dict)):
516516
raise ValueError(
@@ -639,13 +639,13 @@ def convert(
639639
numeric: bool = True,
640640
timedelta: bool = True,
641641
coerce: bool = False,
642-
):
642+
) -> List["Block"]:
643643
"""
644644
attempt to coerce any object types to better types return a copy
645645
of the block (if copy = True) by definition we are not an ObjectBlock
646646
here!
647647
"""
648-
return self.copy() if copy else self
648+
return [self.copy()] if copy else [self]
649649

650650
def _can_hold_element(self, element: Any) -> bool:
651651
""" require the same dtype as ourselves """
@@ -788,7 +788,9 @@ def replace(
788788
convert=convert,
789789
)
790790
if convert:
791-
blocks = [b.convert(numeric=False, copy=not inplace) for b in blocks]
791+
blocks = extend_blocks(
792+
[b.convert(numeric=False, copy=not inplace) for b in blocks]
793+
)
792794
return blocks
793795

794796
def _replace_single(self, *args, **kwargs):
@@ -2461,12 +2463,10 @@ def convert(
24612463
numeric: bool = True,
24622464
timedelta: bool = True,
24632465
coerce: bool = False,
2464-
):
2466+
) -> List["Block"]:
24652467
"""
24662468
attempt to coerce any object types to better types return a copy of
24672469
the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
2468-
2469-
can return multiple blocks!
24702470
"""
24712471
# operate column-by-column
24722472
def f(mask, val, idx):
@@ -2639,8 +2639,10 @@ def re_replacer(s):
26392639
# convert
26402640
block = self.make_block(new_values)
26412641
if convert:
2642-
block = block.convert(numeric=False)
2643-
return block
2642+
nbs = block.convert(numeric=False)
2643+
else:
2644+
nbs = [block]
2645+
return nbs
26442646

26452647
def _replace_coerce(
26462648
self, to_replace, value, inplace=True, regex=False, convert=False, mask=None
@@ -2669,7 +2671,7 @@ def _replace_coerce(
26692671
A new block if there is anything to replace or the original block.
26702672
"""
26712673
if mask.any():
2672-
block = super()._replace_coerce(
2674+
nbs = super()._replace_coerce(
26732675
to_replace=to_replace,
26742676
value=value,
26752677
inplace=inplace,
@@ -2678,11 +2680,11 @@ def _replace_coerce(
26782680
mask=mask,
26792681
)
26802682
if convert:
2681-
block = [b.convert(numeric=False, copy=True) for b in block]
2682-
return block
2683+
nbs = extend_blocks([b.convert(numeric=False, copy=True) for b in nbs])
2684+
return nbs
26832685
if convert:
2684-
return [self.convert(numeric=False, copy=True)]
2685-
return self
2686+
return self.convert(numeric=False, copy=True)
2687+
return [self]
26862688

26872689

26882690
class CategoricalBlock(ExtensionBlock):

0 commit comments

Comments
 (0)