|
7 | 7 |
|
8 | 8 | import numpy as np |
9 | 9 |
|
10 | | -from pandas._libs import algos |
| 10 | +from pandas._libs import Interval, Period, algos |
11 | 11 | from pandas._libs.tslibs import conversion |
12 | 12 | from pandas._typing import ArrayLike, DtypeObj |
13 | 13 |
|
@@ -396,6 +396,9 @@ def is_datetime64_dtype(arr_or_dtype) -> bool: |
396 | 396 | >>> is_datetime64_dtype([1, 2, 3]) |
397 | 397 | False |
398 | 398 | """ |
| 399 | + if isinstance(arr_or_dtype, np.dtype): |
| 400 | + # GH#33400 fastpath for dtype object |
| 401 | + return arr_or_dtype.kind == "M" |
399 | 402 | return _is_dtype_type(arr_or_dtype, classes(np.datetime64)) |
400 | 403 |
|
401 | 404 |
|
@@ -431,6 +434,10 @@ def is_datetime64tz_dtype(arr_or_dtype) -> bool: |
431 | 434 | >>> is_datetime64tz_dtype(s) |
432 | 435 | True |
433 | 436 | """ |
| 437 | + if isinstance(arr_or_dtype, ExtensionDtype): |
| 438 | + # GH#33400 fastpath for dtype object |
| 439 | + return arr_or_dtype.kind == "M" |
| 440 | + |
434 | 441 | if arr_or_dtype is None: |
435 | 442 | return False |
436 | 443 | return DatetimeTZDtype.is_dtype(arr_or_dtype) |
@@ -463,6 +470,10 @@ def is_timedelta64_dtype(arr_or_dtype) -> bool: |
463 | 470 | >>> is_timedelta64_dtype('0 days') |
464 | 471 | False |
465 | 472 | """ |
| 473 | + if isinstance(arr_or_dtype, np.dtype): |
| 474 | + # GH#33400 fastpath for dtype object |
| 475 | + return arr_or_dtype.kind == "m" |
| 476 | + |
466 | 477 | return _is_dtype_type(arr_or_dtype, classes(np.timedelta64)) |
467 | 478 |
|
468 | 479 |
|
@@ -493,6 +504,10 @@ def is_period_dtype(arr_or_dtype) -> bool: |
493 | 504 | >>> is_period_dtype(pd.PeriodIndex([], freq="A")) |
494 | 505 | True |
495 | 506 | """ |
| 507 | + if isinstance(arr_or_dtype, ExtensionDtype): |
| 508 | + # GH#33400 fastpath for dtype object |
| 509 | + return arr_or_dtype.type is Period |
| 510 | + |
496 | 511 | # TODO: Consider making Period an instance of PeriodDtype |
497 | 512 | if arr_or_dtype is None: |
498 | 513 | return False |
@@ -528,6 +543,10 @@ def is_interval_dtype(arr_or_dtype) -> bool: |
528 | 543 | >>> is_interval_dtype(pd.IntervalIndex([interval])) |
529 | 544 | True |
530 | 545 | """ |
| 546 | + if isinstance(arr_or_dtype, ExtensionDtype): |
| 547 | + # GH#33400 fastpath for dtype object |
| 548 | + return arr_or_dtype.type is Interval |
| 549 | + |
531 | 550 | # TODO: Consider making Interval an instance of IntervalDtype |
532 | 551 | if arr_or_dtype is None: |
533 | 552 | return False |
@@ -561,6 +580,10 @@ def is_categorical_dtype(arr_or_dtype) -> bool: |
561 | 580 | >>> is_categorical_dtype(pd.CategoricalIndex([1, 2, 3])) |
562 | 581 | True |
563 | 582 | """ |
| 583 | + if isinstance(arr_or_dtype, ExtensionDtype): |
| 584 | + # GH#33400 fastpath for dtype object |
| 585 | + return arr_or_dtype.name == "category" |
| 586 | + |
564 | 587 | if arr_or_dtype is None: |
565 | 588 | return False |
566 | 589 | return CategoricalDtype.is_dtype(arr_or_dtype) |
@@ -938,6 +961,10 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool: |
938 | 961 | >>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")) |
939 | 962 | True |
940 | 963 | """ |
| 964 | + if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)): |
| 965 | + # GH#33400 fastpath for dtype object |
| 966 | + return arr_or_dtype.kind == "M" |
| 967 | + |
941 | 968 | if arr_or_dtype is None: |
942 | 969 | return False |
943 | 970 | return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype) |
|
0 commit comments