Skip to content

TYP: consistent return types blocks #36967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,11 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]

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

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

values = self.values

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

nv = maybe_downcast_to_dtype(values, dtypes)
return self.make_block(nv)
return [self.make_block(nv)]

# ndim > 1
if dtypes is None:
return self
return [self]

if not (dtypes == "infer" or isinstance(dtypes, dict)):
raise ValueError(
Expand Down Expand Up @@ -639,13 +639,13 @@ def convert(
numeric: bool = True,
timedelta: bool = True,
coerce: bool = False,
):
) -> List["Block"]:
"""
attempt to coerce any object types to better types return a copy
of the block (if copy = True) by definition we are not an ObjectBlock
here!
"""
return self.copy() if copy else self
return [self.copy()] if copy else [self]

def _can_hold_element(self, element: Any) -> bool:
""" require the same dtype as ourselves """
Expand Down Expand Up @@ -788,7 +788,9 @@ def replace(
convert=convert,
)
if convert:
blocks = [b.convert(numeric=False, copy=not inplace) for b in blocks]
blocks = extend_blocks(
[b.convert(numeric=False, copy=not inplace) for b in blocks]
)
return blocks

def _replace_single(self, *args, **kwargs):
Expand Down Expand Up @@ -2461,12 +2463,10 @@ def convert(
numeric: bool = True,
timedelta: bool = True,
coerce: bool = False,
):
) -> List["Block"]:
"""
attempt to coerce any object types to better types return a copy of
the block (if copy = True) by definition we ARE an ObjectBlock!!!!!

can return multiple blocks!
"""
# operate column-by-column
def f(mask, val, idx):
Expand Down Expand Up @@ -2639,8 +2639,10 @@ def re_replacer(s):
# convert
block = self.make_block(new_values)
if convert:
block = block.convert(numeric=False)
return block
nbs = block.convert(numeric=False)
else:
nbs = [block]
return nbs

def _replace_coerce(
self, to_replace, value, inplace=True, regex=False, convert=False, mask=None
Expand Down Expand Up @@ -2669,7 +2671,7 @@ def _replace_coerce(
A new block if there is anything to replace or the original block.
"""
if mask.any():
block = super()._replace_coerce(
nbs = super()._replace_coerce(
to_replace=to_replace,
value=value,
inplace=inplace,
Expand All @@ -2678,11 +2680,11 @@ def _replace_coerce(
mask=mask,
)
if convert:
block = [b.convert(numeric=False, copy=True) for b in block]
return block
nbs = extend_blocks([b.convert(numeric=False, copy=True) for b in nbs])
return nbs
if convert:
return [self.convert(numeric=False, copy=True)]
return self
return self.convert(numeric=False, copy=True)
return [self]


class CategoricalBlock(ExtensionBlock):
Expand Down