diff --git a/pandas/_libs/__init__.py b/pandas/_libs/__init__.py index 141ca0645b906..f119e280f5867 100644 --- a/pandas/_libs/__init__.py +++ b/pandas/_libs/__init__.py @@ -6,9 +6,11 @@ "Timedelta", "Timestamp", "iNaT", + "Interval", ] +from pandas._libs.interval import Interval from pandas._libs.tslibs import ( NaT, NaTType, diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index b2301ab0190c7..76aefd9d5ba8f 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -7,7 +7,7 @@ import numpy as np -from pandas._libs import algos +from pandas._libs import Interval, Period, algos from pandas._libs.tslibs import conversion from pandas._typing import ArrayLike, DtypeObj @@ -396,6 +396,9 @@ def is_datetime64_dtype(arr_or_dtype) -> bool: >>> is_datetime64_dtype([1, 2, 3]) False """ + if isinstance(arr_or_dtype, np.dtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.kind == "M" return _is_dtype_type(arr_or_dtype, classes(np.datetime64)) @@ -431,6 +434,10 @@ def is_datetime64tz_dtype(arr_or_dtype) -> bool: >>> is_datetime64tz_dtype(s) True """ + if isinstance(arr_or_dtype, ExtensionDtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.kind == "M" + if arr_or_dtype is None: return False return DatetimeTZDtype.is_dtype(arr_or_dtype) @@ -463,6 +470,10 @@ def is_timedelta64_dtype(arr_or_dtype) -> bool: >>> is_timedelta64_dtype('0 days') False """ + if isinstance(arr_or_dtype, np.dtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.kind == "m" + return _is_dtype_type(arr_or_dtype, classes(np.timedelta64)) @@ -493,6 +504,10 @@ def is_period_dtype(arr_or_dtype) -> bool: >>> is_period_dtype(pd.PeriodIndex([], freq="A")) True """ + if isinstance(arr_or_dtype, ExtensionDtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.type is Period + # TODO: Consider making Period an instance of PeriodDtype if arr_or_dtype is None: return False @@ -528,6 +543,10 @@ def is_interval_dtype(arr_or_dtype) -> bool: >>> is_interval_dtype(pd.IntervalIndex([interval])) True """ + if isinstance(arr_or_dtype, ExtensionDtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.type is Interval + # TODO: Consider making Interval an instance of IntervalDtype if arr_or_dtype is None: return False @@ -561,6 +580,10 @@ def is_categorical_dtype(arr_or_dtype) -> bool: >>> is_categorical_dtype(pd.CategoricalIndex([1, 2, 3])) True """ + if isinstance(arr_or_dtype, ExtensionDtype): + # GH#33400 fastpath for dtype object + return arr_or_dtype.name == "category" + if arr_or_dtype is None: return False return CategoricalDtype.is_dtype(arr_or_dtype) @@ -938,6 +961,10 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool: >>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) True """ + if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)): + # GH#33400 fastpath for dtype object + return arr_or_dtype.kind == "M" + if arr_or_dtype is None: return False return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype)