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

COMPAT: add back dummy CategoricalBlock class #40582

Merged
merged 3 commits into from
Mar 23, 2021
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
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