Skip to content

PERF: fastpaths in is_foo_dtype checks #33400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/_libs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"Timedelta",
"Timestamp",
"iNaT",
"Interval",
]


from pandas._libs.interval import Interval
from pandas._libs.tslibs import (
NaT,
NaTType,
Expand Down
29 changes: 28 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down