|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 |
|
9 | | -from pandas._libs import NaT, algos as libalgos, internals as libinternals, lib, writers |
| 9 | +from pandas._libs import ( |
| 10 | + Interval, |
| 11 | + NaT, |
| 12 | + Period, |
| 13 | + Timestamp, |
| 14 | + algos as libalgos, |
| 15 | + internals as libinternals, |
| 16 | + lib, |
| 17 | + writers, |
| 18 | +) |
10 | 19 | from pandas._libs.internals import BlockPlacement |
11 | 20 | from pandas._libs.tslibs import conversion |
12 | 21 | from pandas._libs.tslibs.timezones import tz_compare |
|
41 | 50 | is_float_dtype, |
42 | 51 | is_integer, |
43 | 52 | is_integer_dtype, |
44 | | - is_interval_dtype, |
45 | 53 | is_list_like, |
46 | 54 | is_object_dtype, |
47 | | - is_period_dtype, |
48 | 55 | is_re, |
49 | 56 | is_re_compilable, |
50 | 57 | is_sparse, |
51 | 58 | is_timedelta64_dtype, |
52 | 59 | pandas_dtype, |
53 | 60 | ) |
54 | | -from pandas.core.dtypes.dtypes import ExtensionDtype |
| 61 | +from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype |
55 | 62 | from pandas.core.dtypes.generic import ABCDataFrame, ABCIndex, ABCPandasArray, ABCSeries |
56 | 63 | from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, isna_compat |
57 | 64 |
|
@@ -2629,36 +2636,38 @@ def get_block_type(values, dtype=None): |
2629 | 2636 | ------- |
2630 | 2637 | cls : class, subclass of Block |
2631 | 2638 | """ |
| 2639 | + # We use vtype and kind checks because they are much more performant |
| 2640 | + # than is_foo_dtype |
2632 | 2641 | dtype = dtype or values.dtype |
2633 | 2642 | vtype = dtype.type |
| 2643 | + kind = dtype.kind |
2634 | 2644 |
|
2635 | 2645 | cls: Type[Block] |
2636 | 2646 |
|
2637 | 2647 | if is_sparse(dtype): |
2638 | 2648 | # Need this first(ish) so that Sparse[datetime] is sparse |
2639 | 2649 | cls = ExtensionBlock |
2640 | | - elif is_categorical_dtype(values.dtype): |
| 2650 | + elif isinstance(dtype, CategoricalDtype): |
2641 | 2651 | cls = CategoricalBlock |
2642 | | - elif issubclass(vtype, np.datetime64): |
2643 | | - assert not is_datetime64tz_dtype(values.dtype) |
2644 | | - cls = DatetimeBlock |
2645 | | - elif is_datetime64tz_dtype(values.dtype): |
| 2652 | + elif vtype is Timestamp: |
2646 | 2653 | cls = DatetimeTZBlock |
2647 | | - elif is_interval_dtype(dtype) or is_period_dtype(dtype): |
| 2654 | + elif vtype is Interval or vtype is Period: |
2648 | 2655 | cls = ObjectValuesExtensionBlock |
2649 | | - elif is_extension_array_dtype(values.dtype): |
| 2656 | + elif isinstance(dtype, ExtensionDtype): |
2650 | 2657 | # Note: need to be sure PandasArray is unwrapped before we get here |
2651 | 2658 | cls = ExtensionBlock |
2652 | | - elif issubclass(vtype, np.floating): |
2653 | | - cls = FloatBlock |
2654 | | - elif issubclass(vtype, np.timedelta64): |
2655 | | - assert issubclass(vtype, np.integer) |
| 2659 | + |
| 2660 | + elif kind == "M": |
| 2661 | + cls = DatetimeBlock |
| 2662 | + elif kind == "m": |
2656 | 2663 | cls = TimeDeltaBlock |
2657 | | - elif issubclass(vtype, np.complexfloating): |
| 2664 | + elif kind == "f": |
| 2665 | + cls = FloatBlock |
| 2666 | + elif kind == "c": |
2658 | 2667 | cls = ComplexBlock |
2659 | | - elif issubclass(vtype, np.integer): |
| 2668 | + elif kind == "i" or kind == "u": |
2660 | 2669 | cls = IntBlock |
2661 | | - elif dtype == np.bool_: |
| 2670 | + elif kind == "b": |
2662 | 2671 | cls = BoolBlock |
2663 | 2672 | else: |
2664 | 2673 | cls = ObjectBlock |
|
0 commit comments