Skip to content
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

PERF: Block.apply #43609

Merged
merged 3 commits into from
Sep 17, 2021
Merged
Changes from 2 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
23 changes: 10 additions & 13 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ def apply(self, func, **kwargs) -> list[Block]:
apply the function to my values; return a block if we are not
one
"""
with np.errstate(all="ignore"):
jreback marked this conversation as resolved.
Show resolved Hide resolved
result = func(self.values, **kwargs)
result = func(self.values, **kwargs)

return self._split_op_result(result)

Expand All @@ -400,9 +399,9 @@ def reduce(self, func, ignore_failures: bool = False) -> list[Block]:
return [nb]

@final
def _split_op_result(self, result) -> list[Block]:
def _split_op_result(self, result: ArrayLike) -> list[Block]:
# See also: split_and_operate
if is_extension_array_dtype(result) and result.ndim > 1:
if result.ndim > 1 and isinstance(result.dtype, ExtensionDtype):
# TODO(EA2D): unnecessary with 2D EAs
# if we get a 2D ExtensionArray, we need to split it into 1D pieces
nbs = []
Expand All @@ -416,11 +415,9 @@ def _split_op_result(self, result) -> list[Block]:
nbs.append(block)
return nbs

if not isinstance(result, Block):
result = maybe_coerce_values(result)
result = self.make_block(result)
nb = self.make_block(result)

return [result]
return [nb]

def fillna(
self, value, limit=None, inplace: bool = False, downcast=None
Expand Down Expand Up @@ -475,7 +472,8 @@ def _split(self) -> list[Block]:
for i, ref_loc in enumerate(self._mgr_locs):
vals = self.values[slice(i, i + 1)]

nb = self.make_block(vals, BlockPlacement(ref_loc))
bp = BlockPlacement(ref_loc)
nb = type(self)(vals, placement=bp, ndim=2)
new_blocks.append(nb)
return new_blocks

Expand Down Expand Up @@ -648,7 +646,7 @@ def copy(self, deep: bool = True):
values = self.values
if deep:
values = values.copy()
return self.make_block_same_class(values)
return type(self)(values, placement=self._mgr_locs, ndim=self.ndim)

# ---------------------------------------------------------------------
# Replace
Expand Down Expand Up @@ -1957,16 +1955,15 @@ def get_block_type(dtype: DtypeObj):
return cls


def new_block(values, placement, *, ndim: int, klass=None) -> Block:
def new_block(values, placement, *, ndim: int) -> Block:
# caller is responsible for ensuring values is NOT a PandasArray

if not isinstance(placement, BlockPlacement):
placement = BlockPlacement(placement)

check_ndim(values, placement, ndim)

if klass is None:
klass = get_block_type(values.dtype)
klass = get_block_type(values.dtype)

values = maybe_coerce_values(values)
return klass(values, ndim=ndim, placement=placement)
Expand Down