Skip to content

Commit fb582e5

Browse files
jbrockmendelCloseChoice
authored andcommitted
PERF: fastpaths in is_foo_dtype checks (pandas-dev#33400)
* PERF: implement dtype-only dtype checks * remove strict versions
1 parent 33052d8 commit fb582e5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pandas/_libs/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"Timedelta",
77
"Timestamp",
88
"iNaT",
9+
"Interval",
910
]
1011

1112

13+
from pandas._libs.interval import Interval
1214
from pandas._libs.tslibs import (
1315
NaT,
1416
NaTType,

pandas/core/dtypes/common.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99

10-
from pandas._libs import algos
10+
from pandas._libs import Interval, Period, algos
1111
from pandas._libs.tslibs import conversion
1212
from pandas._typing import ArrayLike, DtypeObj
1313

@@ -396,6 +396,9 @@ def is_datetime64_dtype(arr_or_dtype) -> bool:
396396
>>> is_datetime64_dtype([1, 2, 3])
397397
False
398398
"""
399+
if isinstance(arr_or_dtype, np.dtype):
400+
# GH#33400 fastpath for dtype object
401+
return arr_or_dtype.kind == "M"
399402
return _is_dtype_type(arr_or_dtype, classes(np.datetime64))
400403

401404

@@ -431,6 +434,10 @@ def is_datetime64tz_dtype(arr_or_dtype) -> bool:
431434
>>> is_datetime64tz_dtype(s)
432435
True
433436
"""
437+
if isinstance(arr_or_dtype, ExtensionDtype):
438+
# GH#33400 fastpath for dtype object
439+
return arr_or_dtype.kind == "M"
440+
434441
if arr_or_dtype is None:
435442
return False
436443
return DatetimeTZDtype.is_dtype(arr_or_dtype)
@@ -463,6 +470,10 @@ def is_timedelta64_dtype(arr_or_dtype) -> bool:
463470
>>> is_timedelta64_dtype('0 days')
464471
False
465472
"""
473+
if isinstance(arr_or_dtype, np.dtype):
474+
# GH#33400 fastpath for dtype object
475+
return arr_or_dtype.kind == "m"
476+
466477
return _is_dtype_type(arr_or_dtype, classes(np.timedelta64))
467478

468479

@@ -493,6 +504,10 @@ def is_period_dtype(arr_or_dtype) -> bool:
493504
>>> is_period_dtype(pd.PeriodIndex([], freq="A"))
494505
True
495506
"""
507+
if isinstance(arr_or_dtype, ExtensionDtype):
508+
# GH#33400 fastpath for dtype object
509+
return arr_or_dtype.type is Period
510+
496511
# TODO: Consider making Period an instance of PeriodDtype
497512
if arr_or_dtype is None:
498513
return False
@@ -528,6 +543,10 @@ def is_interval_dtype(arr_or_dtype) -> bool:
528543
>>> is_interval_dtype(pd.IntervalIndex([interval]))
529544
True
530545
"""
546+
if isinstance(arr_or_dtype, ExtensionDtype):
547+
# GH#33400 fastpath for dtype object
548+
return arr_or_dtype.type is Interval
549+
531550
# TODO: Consider making Interval an instance of IntervalDtype
532551
if arr_or_dtype is None:
533552
return False
@@ -561,6 +580,10 @@ def is_categorical_dtype(arr_or_dtype) -> bool:
561580
>>> is_categorical_dtype(pd.CategoricalIndex([1, 2, 3]))
562581
True
563582
"""
583+
if isinstance(arr_or_dtype, ExtensionDtype):
584+
# GH#33400 fastpath for dtype object
585+
return arr_or_dtype.name == "category"
586+
564587
if arr_or_dtype is None:
565588
return False
566589
return CategoricalDtype.is_dtype(arr_or_dtype)
@@ -938,6 +961,10 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
938961
>>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]"))
939962
True
940963
"""
964+
if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)):
965+
# GH#33400 fastpath for dtype object
966+
return arr_or_dtype.kind == "M"
967+
941968
if arr_or_dtype is None:
942969
return False
943970
return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype)

0 commit comments

Comments
 (0)