Skip to content

Commit 37a0388

Browse files
Avoid deprecated pandas is_categorical_dtype API (#204)
1 parent f43e282 commit 37a0388

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

patsy/util.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,16 @@
4141
# pandas.
4242
have_pandas_categorical = (have_pandas and hasattr(pandas, "Categorical"))
4343
if not have_pandas:
44-
have_pandas_categorical_dtype = False
4544
_pandas_is_categorical_dtype = None
4645
else:
47-
if hasattr(pandas, "api"):
48-
# This is available starting in pandas v0.19.0
49-
have_pandas_categorical_dtype = True
46+
if hasattr(pandas, "CategoricalDtype"): # pandas >= 0.25
47+
_pandas_is_categorical_dtype = lambda x: isinstance(getattr(x, "dtype", x), pandas.CategoricalDtype)
48+
elif hasattr(pandas, "api"): # pandas >= 0.19
5049
_pandas_is_categorical_dtype = getattr(pandas.api.types, "is_categorical_dtype", None)
51-
# pandas.api.types._pandas_is_categorical_dtype is deprecated in pandas v2.1.0
52-
if _pandas_is_categorical_dtype is not None:
53-
import warnings
54-
with warnings.catch_warnings(record=True) as w:
55-
_pandas_is_categorical_dtype(int)
56-
if len(w) > 0 and issubclass(w[-1].category, FutureWarning):
57-
_pandas_is_categorical_dtype = None
58-
else:
59-
_pandas_is_categorical_dtype = lambda x: isinstance(x, pandas.CategoricalDType)
60-
else:
61-
# This is needed for pandas v0.18.0 and earlier
50+
else: # pandas <=0.18
6251
_pandas_is_categorical_dtype = getattr(pandas.core.common,
6352
"is_categorical_dtype", None)
64-
have_pandas_categorical_dtype = (_pandas_is_categorical_dtype
65-
is not None)
53+
have_pandas_categorical_dtype = _pandas_is_categorical_dtype is not None
6654

6755

6856
# Passes through Series and DataFrames, call np.asarray() on everything else

0 commit comments

Comments
 (0)