25
25
from pandas ._libs .internals import BlockPlacement
26
26
from pandas ._typing import (
27
27
ArrayLike ,
28
- Dtype ,
29
28
DtypeObj ,
30
29
F ,
31
30
Shape ,
52
51
is_list_like ,
53
52
is_sparse ,
54
53
is_string_dtype ,
55
- pandas_dtype ,
56
54
)
57
55
from pandas .core .dtypes .dtypes import (
58
56
CategoricalDtype ,
100
98
TimedeltaArray ,
101
99
)
102
100
from pandas .core .arrays ._mixins import NDArrayBackedExtensionArray
101
+ from pandas .core .arrays .sparse import SparseDtype
103
102
from pandas .core .base import PandasObject
104
103
import pandas .core .common as com
105
104
import pandas .core .computation .expressions as expressions
@@ -326,6 +325,8 @@ def getitem_block_columns(self, slicer, new_mgr_locs: BlockPlacement) -> Block:
326
325
327
326
return type (self )(new_values , new_mgr_locs , self .ndim )
328
327
328
+ # NB: this cannot be made cache_readonly because in libreduction we pin
329
+ # new .values that can have different shape GH#42631
329
330
@property
330
331
def shape (self ) -> Shape :
331
332
return self .values .shape
@@ -1255,7 +1256,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
1255
1256
1256
1257
return result_blocks
1257
1258
1258
- def _unstack (self , unstacker , fill_value , new_placement ):
1259
+ def _unstack (self , unstacker , fill_value , new_placement , allow_fill : bool ):
1259
1260
"""
1260
1261
Return a list of unstacked blocks of self
1261
1262
@@ -1264,6 +1265,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
1264
1265
unstacker : reshape._Unstacker
1265
1266
fill_value : int
1266
1267
Only used in ExtensionBlock._unstack
1268
+ allow_fill : bool
1267
1269
1268
1270
Returns
1269
1271
-------
@@ -1638,7 +1640,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
1638
1640
1639
1641
return [self .make_block_same_class (result )]
1640
1642
1641
- def _unstack (self , unstacker , fill_value , new_placement ):
1643
+ def _unstack (self , unstacker , fill_value , new_placement , allow_fill : bool ):
1642
1644
# ExtensionArray-safe unstack.
1643
1645
# We override ObjectBlock._unstack, which unstacks directly on the
1644
1646
# values of the array. For EA-backed blocks, this would require
@@ -1655,7 +1657,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
1655
1657
blocks = [
1656
1658
# TODO: could cast to object depending on fill_value?
1657
1659
self .make_block_same_class (
1658
- self .values .take (indices , allow_fill = True , fill_value = fill_value ),
1660
+ self .values .take (indices , allow_fill = allow_fill , fill_value = fill_value ),
1659
1661
BlockPlacement (place ),
1660
1662
)
1661
1663
for indices , place in zip (new_values .T , new_placement )
@@ -1842,7 +1844,7 @@ class CategoricalBlock(ExtensionBlock):
1842
1844
# Constructor Helpers
1843
1845
1844
1846
1845
- def maybe_coerce_values (values ) -> ArrayLike :
1847
+ def maybe_coerce_values (values : ArrayLike ) -> ArrayLike :
1846
1848
"""
1847
1849
Input validation for values passed to __init__. Ensure that
1848
1850
any datetime64/timedelta64 dtypes are in nanoseconds. Ensure
@@ -1874,7 +1876,7 @@ def maybe_coerce_values(values) -> ArrayLike:
1874
1876
return values
1875
1877
1876
1878
1877
- def get_block_type (values , dtype : Dtype | None = None ):
1879
+ def get_block_type (values , dtype : DtypeObj | None = None ):
1878
1880
"""
1879
1881
Find the appropriate Block subclass to use for the given values and dtype.
1880
1882
@@ -1889,13 +1891,15 @@ def get_block_type(values, dtype: Dtype | None = None):
1889
1891
"""
1890
1892
# We use vtype and kind checks because they are much more performant
1891
1893
# than is_foo_dtype
1892
- dtype = cast (np .dtype , pandas_dtype (dtype ) if dtype else values .dtype )
1894
+ if dtype is None :
1895
+ dtype = values .dtype
1896
+
1893
1897
vtype = dtype .type
1894
1898
kind = dtype .kind
1895
1899
1896
1900
cls : type [Block ]
1897
1901
1898
- if is_sparse (dtype ):
1902
+ if isinstance (dtype , SparseDtype ):
1899
1903
# Need this first(ish) so that Sparse[datetime] is sparse
1900
1904
cls = ExtensionBlock
1901
1905
elif isinstance (dtype , CategoricalDtype ):
0 commit comments