Skip to content

Commit

Permalink
COMPAT: add back dummy CategoricalBlock class (#40582)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Mar 23, 2021
1 parent 1dab473 commit 79f1801
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pandas/core/internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

__all__ = [
"Block",
"CategoricalBlock",
"NumericBlock",
"DatetimeBlock",
"DatetimeTZBlock",
Expand Down Expand Up @@ -56,6 +57,8 @@ def __getattr__(name: str):
DeprecationWarning,
stacklevel=2,
)
return ExtensionBlock
from pandas.core.internals.blocks import CategoricalBlock

return CategoricalBlock

raise AttributeError(f"module 'pandas.core.internals' has no attribute '{name}'")
11 changes: 8 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ def take_nd(
Take values according to indexer and return them as a block.bb
"""
# algos.take_nd dispatches for DatetimeTZBlock
# algos.take_nd dispatches for DatetimeTZBlock, CategoricalBlock
# so need to preserve types
# sparse is treated like an ndarray, but needs .get_values() shaping

Expand Down Expand Up @@ -1443,7 +1443,7 @@ class ExtensionBlock(Block):
Notes
-----
This holds all 3rd-party extension array types. It's also the immediate
parent class for our internal extension types' blocks.
parent class for our internal extension types' blocks, CategoricalBlock.
ExtensionArrays are limited to 1-D.
"""
Expand Down Expand Up @@ -2017,6 +2017,11 @@ def _can_hold_element(self, element: Any) -> bool:
return True


class CategoricalBlock(ExtensionBlock):
# this Block type is kept for backwards-compatibility
__slots__ = ()


# -----------------------------------------------------------------
# Constructor Helpers

Expand Down Expand Up @@ -2078,7 +2083,7 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
# Need this first(ish) so that Sparse[datetime] is sparse
cls = ExtensionBlock
elif isinstance(dtype, CategoricalDtype):
cls = ExtensionBlock
cls = CategoricalBlock
elif vtype is Timestamp:
cls = DatetimeTZBlock
elif vtype is Interval or vtype is Period:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
)
from pandas.core.internals.blocks import (
Block,
CategoricalBlock,
DatetimeTZBlock,
ExtensionBlock,
ObjectValuesExtensionBlock,
Expand Down Expand Up @@ -1860,6 +1861,13 @@ def _form_blocks(
object_blocks = _simple_blockify(items_dict["ObjectBlock"], np.object_)
blocks.extend(object_blocks)

if len(items_dict["CategoricalBlock"]) > 0:
cat_blocks = [
new_block(array, klass=CategoricalBlock, placement=i, ndim=2)
for i, array in items_dict["CategoricalBlock"]
]
blocks.extend(cat_blocks)

if len(items_dict["ExtensionBlock"]):
external_blocks = [
new_block(array, klass=ExtensionBlock, placement=i, ndim=2)
Expand Down

0 comments on commit 79f1801

Please sign in to comment.