Skip to content

REF/INT: preserve block type in joining when only having a single block #17954

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
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5182,7 +5182,15 @@ def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy):

for placement, join_units in concat_plan:

if is_uniform_join_units(join_units):
if len(join_units) == 1 and not join_units[0].indexers:
b = join_units[0].block
values = b.values
if copy: # and values.base is not None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I first had this with and values.base is not None: but that failed one test in merge. But I copied that logic from concatenate_join_units:

https://github.com/jorisvandenbossche/pandas/blob/2125ac09726cfc720def79e836398ed04ecae04a/pandas/core/internals.py#L5332

Not sure why there it is needed, but here it fails.

values = values.copy()
elif not copy:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason for this special case? IOW, why can't you handle this in concat_same_type (maybe pass in copy flag if that helps), IOW you can test there that you only have 1 block and write the same code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the separate special case because it occurs in different cases (and so how it is here, is more close to the original code), but could add it to the other special case. But, that makes concat_same_type more complex. Currently it does not need to bother about copying the data or not in this special case of to_concat values of 1, because concatenate always takes a copy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I guess. Still like to come back in and re-org this to make as clear as possible during next release. Do appreciate the support for CustomBlocks, though worry we are adding even more technical debt here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah what I would actually do is define a _concatentor for the base case (e.g. where we have staticmethod(np.concatenate); in pandas/core/dtypes/concat.py, call it _concat_arrays or something. This (and you would have to modify all the others to take a copy= kwarg, then this code become much simpler & less special cased.

Then all of the concat logic is in a single place and copies are handled there. Pls create an issue for 0.22 for this (you may already have one).

values = values.view()
b = b.make_block_same_class(values, placement=placement)
elif is_uniform_join_units(join_units):
b = join_units[0].block.concat_same_type(
[ju.block for ju in join_units], placement=placement)
else:
Expand Down
29 changes: 21 additions & 8 deletions pandas/tests/internals/test_external_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pandas.core.internals import (
Block, BlockManager, SingleBlockManager, NonConsolidatableMixIn)

import pytest


class CustomBlock(NonConsolidatableMixIn, Block):

Expand All @@ -25,6 +27,17 @@ def concat_same_type(self, to_concat, placement=None):
values, placement=placement or slice(0, len(values), 1))


@pytest.fixture
def df():
df1 = pd.DataFrame({'a': [1, 2, 3]})
blocks = df1._data.blocks
values = np.arange(3, dtype='int64')
custom_block = CustomBlock(values, placement=slice(1, 2))
blocks = blocks + (custom_block,)
block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df1.index])
return pd.DataFrame(block_manager)


def test_custom_repr():
values = np.arange(3, dtype='int64')

Expand All @@ -51,14 +64,14 @@ def test_concat_series():
assert isinstance(res._data.blocks[0], CustomBlock)


def test_concat_dataframe():
def test_concat_dataframe(df):
# GH17728
df = pd.DataFrame({'a': [1, 2, 3]})
blocks = df._data.blocks
values = np.arange(3, dtype='int64')
custom_block = CustomBlock(values, placement=slice(1, 2))
blocks = blocks + (custom_block, )
block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df.index])
df = pd.DataFrame(block_manager)
res = pd.concat([df, df])
assert isinstance(res._data.blocks[1], CustomBlock)


def test_concat_axis1(df):
# GH17954
df2 = pd.DataFrame({'c': [.1, .2, .3]})
res = pd.concat([df, df2], axis=1)
assert isinstance(res._data.blocks[1], CustomBlock)