Skip to content

CLN: unnecessary checks in internals.concat #41189

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 2 commits into from
Apr 29, 2021
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
31 changes: 16 additions & 15 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

import pandas.core.algorithms as algos
from pandas.core.arrays import (
Categorical,
DatetimeArray,
ExtensionArray,
)
Expand Down Expand Up @@ -193,17 +192,17 @@ def concatenate_managers(
blocks = []

for placement, join_units in concat_plan:
unit = join_units[0]
blk = unit.block

if len(join_units) == 1 and not join_units[0].indexers:
b = join_units[0].block
values = b.values
values = blk.values
if copy:
values = values.copy()
else:
values = values.view()
b = b.make_block_same_class(values, placement=placement)
fastpath = True
elif _is_uniform_join_units(join_units):
blk = join_units[0].block
vals = [ju.block.values for ju in join_units]

if not blk.is_extension:
Expand All @@ -218,14 +217,16 @@ def concatenate_managers(

values = ensure_wrapped_if_datetimelike(values)

if blk.values.dtype == values.dtype:
# Fast-path
b = blk.make_block_same_class(values, placement=placement)
else:
b = new_block(values, placement=placement, ndim=blk.ndim)
fastpath = blk.values.dtype == values.dtype
else:
values = _concatenate_join_units(join_units, concat_axis, copy=copy)
fastpath = False

if fastpath:
b = blk.make_block_same_class(values, placement=placement)
else:
new_values = _concatenate_join_units(join_units, concat_axis, copy=copy)
b = new_block(new_values, placement=placement, ndim=len(axes))
b = new_block(values, placement=placement, ndim=len(axes))

blocks.append(b)

return BlockManager(tuple(blocks), axes)
Expand Down Expand Up @@ -445,12 +446,10 @@ def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike:
# preserve these for validation in concat_compat
return self.block.values

if self.block.is_bool and not isinstance(self.block.values, Categorical):
if self.block.is_bool:
# External code requested filling/upcasting, bool values must
# be upcasted to object to avoid being upcasted to numeric.
values = self.block.astype(np.object_).values
elif self.block.is_extension:
values = self.block.values
else:
# No dtype upcasting is done here, it will be performed during
# concatenation itself.
Expand Down Expand Up @@ -533,9 +532,11 @@ def _dtype_to_na_value(dtype: DtypeObj, has_none_blocks: bool):
elif dtype.kind in ["f", "c"]:
return dtype.type("NaN")
elif dtype.kind == "b":
# different from missing.na_value_for_dtype
return None
elif dtype.kind in ["i", "u"]:
if not has_none_blocks:
# different from missing.na_value_for_dtype
return None
return np.nan
elif dtype.kind == "O":
Expand Down