Skip to content

REF: merge NonConsolidateableMixin into ExtensionArray #32714

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
Mar 16, 2020
Merged
Show file tree
Hide file tree
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
43 changes: 18 additions & 25 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,12 +1598,22 @@ def _replace_coerce(
return self


class NonConsolidatableMixIn:
""" hold methods for the nonconsolidatable blocks """
class ExtensionBlock(Block):
"""
Block for holding extension types.

Notes
-----
This holds all 3rd-party extension array types. It's also the immediate
parent class for our internal extension types' blocks, CategoricalBlock.

ExtensionArrays are limited to 1-D.
"""

_can_consolidate = False
_verify_integrity = False
_validate_ndim = False
is_extension = True

def __init__(self, values, placement, ndim=None):
"""
Expand All @@ -1614,6 +1624,8 @@ def __init__(self, values, placement, ndim=None):
This will call continue to call __init__ for the other base
classes mixed in with this Mixin.
"""
values = self._maybe_coerce_values(values)

# Placement must be converted to BlockPlacement so that we can check
# its length
if not isinstance(placement, libinternals.BlockPlacement):
Expand All @@ -1627,6 +1639,10 @@ def __init__(self, values, placement, ndim=None):
ndim = 2
super().__init__(values, placement, ndim=ndim)

if self.ndim == 2 and len(self.mgr_locs) != 1:
# TODO(2DEA): check unnecessary with 2D EAs
raise AssertionError("block.size != values.size")

@property
def shape(self):
if self.ndim == 1:
Expand Down Expand Up @@ -1722,29 +1738,6 @@ def _get_unstack_items(self, unstacker, new_columns):
mask = mask.any(0)
return new_placement, new_values, mask


class ExtensionBlock(NonConsolidatableMixIn, Block):
"""
Block for holding extension types.

Notes
-----
This holds all 3rd-party extension array types. It's also the immediate
parent class for our internal extension types' blocks, CategoricalBlock.

ExtensionArrays are limited to 1-D.
"""

is_extension = True

def __init__(self, values, placement, ndim=None):
values = self._maybe_coerce_values(values)
super().__init__(values, placement, ndim)

if self.ndim == 2 and len(self.mgr_locs) != 1:
# TODO(2DEA): check unnecessary with 2D EAs
raise AssertionError("block.size != values.size")

def _maybe_coerce_values(self, values):
"""
Unbox to an extension array.
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/test_external_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import pandas as pd
from pandas.core.internals import BlockManager, SingleBlockManager
from pandas.core.internals.blocks import Block, NonConsolidatableMixIn
from pandas.core.internals.blocks import ExtensionBlock


class CustomBlock(NonConsolidatableMixIn, Block):
class CustomBlock(ExtensionBlock):

_holder = np.ndarray
_can_hold_na = False

def concat_same_type(self, to_concat, placement=None):
"""
Expand Down