Skip to content

Commit 5c15588

Browse files
authored
PERF: get_block_type (#52109)
* PERF: get_block_type (for EA's mostly) * updates
1 parent f2cd62a commit 5c15588

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

pandas/core/internals/blocks.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
PeriodArray,
107107
TimedeltaArray,
108108
)
109-
from pandas.core.arrays.sparse import SparseDtype
110109
from pandas.core.base import PandasObject
111110
import pandas.core.common as com
112111
from pandas.core.computation import expressions
@@ -2329,7 +2328,7 @@ def maybe_coerce_values(values: ArrayLike) -> ArrayLike:
23292328
return values
23302329

23312330

2332-
def get_block_type(dtype: DtypeObj):
2331+
def get_block_type(dtype: DtypeObj) -> type[Block]:
23332332
"""
23342333
Find the appropriate Block subclass to use for the given values and dtype.
23352334
@@ -2341,30 +2340,23 @@ def get_block_type(dtype: DtypeObj):
23412340
-------
23422341
cls : class, subclass of Block
23432342
"""
2344-
# We use kind checks because it is much more performant
2345-
# than is_foo_dtype
2346-
kind = dtype.kind
2347-
2348-
cls: type[Block]
2349-
2350-
if isinstance(dtype, SparseDtype):
2351-
# Need this first(ish) so that Sparse[datetime] is sparse
2352-
cls = ExtensionBlock
2353-
elif isinstance(dtype, DatetimeTZDtype):
2354-
cls = DatetimeTZBlock
2343+
if isinstance(dtype, DatetimeTZDtype):
2344+
return DatetimeTZBlock
23552345
elif isinstance(dtype, PeriodDtype):
2356-
cls = NDArrayBackedExtensionBlock
2346+
return NDArrayBackedExtensionBlock
23572347
elif isinstance(dtype, ExtensionDtype):
23582348
# Note: need to be sure PandasArray is unwrapped before we get here
2359-
cls = ExtensionBlock
2349+
return ExtensionBlock
23602350

2361-
elif kind in ["M", "m"]:
2362-
cls = DatetimeLikeBlock
2363-
elif kind in ["f", "c", "i", "u", "b"]:
2364-
cls = NumericBlock
2365-
else:
2366-
cls = ObjectBlock
2367-
return cls
2351+
# We use kind checks because it is much more performant
2352+
# than is_foo_dtype
2353+
kind = dtype.kind
2354+
if kind in "Mm":
2355+
return DatetimeLikeBlock
2356+
elif kind in "fciub":
2357+
return NumericBlock
2358+
2359+
return ObjectBlock
23682360

23692361

23702362
def new_block_2d(

0 commit comments

Comments
 (0)