Skip to content

Commit 2cea646

Browse files
committed
BUG: Avoid exceptions in is_dtype
1 parent 3e3256b commit 2cea646

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pandas/core/dtypes/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import numpy as np
33

44
from pandas import compat
5+
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass, ABCDataFrame
56
from pandas.errors import AbstractMethodError
67

78

@@ -83,7 +84,12 @@ def is_dtype(cls, dtype):
8384
"""
8485
dtype = getattr(dtype, 'dtype', dtype)
8586

86-
if isinstance(dtype, np.dtype):
87+
if isinstance(dtype, (ABCSeries, ABCIndexClass,
88+
ABCDataFrame, np.dtype)):
89+
# https://github.com/pandas-dev/pandas/issues/22960
90+
# avoid passing data to `construct_from_string`. This could
91+
# cause a FutureWarning from numpy about failing elementwise
92+
# comparison from, e.g., comparing DataFrame == 'category'.
8793
return False
8894
elif dtype is None:
8995
return False

pandas/tests/dtypes/test_dtypes.py

+20
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,23 @@ def test_registry_find(dtype, expected):
815815
('datetime64[ns, US/Eastern]', DatetimeTZDtype('ns', 'US/Eastern'))])
816816
def test_pandas_registry_find(dtype, expected):
817817
assert _pandas_registry.find(dtype) == expected
818+
819+
820+
@pytest.mark.parametrize("check", [
821+
is_categorical_dtype,
822+
is_datetime64tz_dtype,
823+
is_period_dtype,
824+
is_datetime64_ns_dtype,
825+
is_datetime64_dtype,
826+
is_interval_dtype,
827+
is_datetime64_any_dtype,
828+
is_string_dtype,
829+
is_bool_dtype,
830+
])
831+
def test_is_dtype_no_warning(check):
832+
data = pd.DataFrame({"A": [1, 2]})
833+
with tm.assert_produces_warning(None):
834+
check(data)
835+
836+
with tm.assert_produces_warning(None):
837+
check(data["A"])

0 commit comments

Comments
 (0)