PERF: get_block_type (#52109) · pandas-dev/pandas@5c15588 (original) (raw)

Original file line number Diff line number Diff line change
@@ -106,7 +106,6 @@
106 106 PeriodArray,
107 107 TimedeltaArray,
108 108 )
109 -from pandas.core.arrays.sparse import SparseDtype
110 109 from pandas.core.base import PandasObject
111 110 import pandas.core.common as com
112 111 from pandas.core.computation import expressions
@@ -2329,7 +2328,7 @@ def maybe_coerce_values(values: ArrayLike) -> ArrayLike:
2329 2328 return values
2330 2329
2331 2330
2332 -def get_block_type(dtype: DtypeObj):
2331 +def get_block_type(dtype: DtypeObj) -> type[Block]:
2333 2332 """
2334 2333 Find the appropriate Block subclass to use for the given values and dtype.
2335 2334
@@ -2341,30 +2340,23 @@ def get_block_type(dtype: DtypeObj):
2341 2340 -------
2342 2341 cls : class, subclass of Block
2343 2342 """
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
2355 2345 elif isinstance(dtype, PeriodDtype):
2356 -cls = NDArrayBackedExtensionBlock
2346 +return NDArrayBackedExtensionBlock
2357 2347 elif isinstance(dtype, ExtensionDtype):
2358 2348 # Note: need to be sure PandasArray is unwrapped before we get here
2359 -cls = ExtensionBlock
2349 +return ExtensionBlock
2360 2350
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
2368 2360
2369 2361
2370 2362 def new_block_2d(