Skip to content

Commit 79f1801

Browse files
COMPAT: add back dummy CategoricalBlock class (#40582)
1 parent 1dab473 commit 79f1801

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

pandas/core/internals/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
__all__ = [
2828
"Block",
29+
"CategoricalBlock",
2930
"NumericBlock",
3031
"DatetimeBlock",
3132
"DatetimeTZBlock",
@@ -56,6 +57,8 @@ def __getattr__(name: str):
5657
DeprecationWarning,
5758
stacklevel=2,
5859
)
59-
return ExtensionBlock
60+
from pandas.core.internals.blocks import CategoricalBlock
61+
62+
return CategoricalBlock
6063

6164
raise AttributeError(f"module 'pandas.core.internals' has no attribute '{name}'")

pandas/core/internals/blocks.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ def take_nd(
12441244
Take values according to indexer and return them as a block.bb
12451245
12461246
"""
1247-
# algos.take_nd dispatches for DatetimeTZBlock
1247+
# algos.take_nd dispatches for DatetimeTZBlock, CategoricalBlock
12481248
# so need to preserve types
12491249
# sparse is treated like an ndarray, but needs .get_values() shaping
12501250

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

20192019

2020+
class CategoricalBlock(ExtensionBlock):
2021+
# this Block type is kept for backwards-compatibility
2022+
__slots__ = ()
2023+
2024+
20202025
# -----------------------------------------------------------------
20212026
# Constructor Helpers
20222027

@@ -2078,7 +2083,7 @@ def get_block_type(values, dtype: Optional[Dtype] = None):
20782083
# Need this first(ish) so that Sparse[datetime] is sparse
20792084
cls = ExtensionBlock
20802085
elif isinstance(dtype, CategoricalDtype):
2081-
cls = ExtensionBlock
2086+
cls = CategoricalBlock
20822087
elif vtype is Timestamp:
20832088
cls = DatetimeTZBlock
20842089
elif vtype is Interval or vtype is Period:

pandas/core/internals/managers.py

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
)
6868
from pandas.core.internals.blocks import (
6969
Block,
70+
CategoricalBlock,
7071
DatetimeTZBlock,
7172
ExtensionBlock,
7273
ObjectValuesExtensionBlock,
@@ -1860,6 +1861,13 @@ def _form_blocks(
18601861
object_blocks = _simple_blockify(items_dict["ObjectBlock"], np.object_)
18611862
blocks.extend(object_blocks)
18621863

1864+
if len(items_dict["CategoricalBlock"]) > 0:
1865+
cat_blocks = [
1866+
new_block(array, klass=CategoricalBlock, placement=i, ndim=2)
1867+
for i, array in items_dict["CategoricalBlock"]
1868+
]
1869+
blocks.extend(cat_blocks)
1870+
18631871
if len(items_dict["ExtensionBlock"]):
18641872
external_blocks = [
18651873
new_block(array, klass=ExtensionBlock, placement=i, ndim=2)

0 commit comments

Comments
 (0)