Skip to content

Commit

Permalink
Avoid deprecated pandas is_categorical_dtype API (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop authored Jan 4, 2024
1 parent f43e282 commit 37a0388
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions patsy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,16 @@
# pandas.
have_pandas_categorical = (have_pandas and hasattr(pandas, "Categorical"))
if not have_pandas:
have_pandas_categorical_dtype = False
_pandas_is_categorical_dtype = None
else:
if hasattr(pandas, "api"):
# This is available starting in pandas v0.19.0
have_pandas_categorical_dtype = True
if hasattr(pandas, "CategoricalDtype"): # pandas >= 0.25
_pandas_is_categorical_dtype = lambda x: isinstance(getattr(x, "dtype", x), pandas.CategoricalDtype)
elif hasattr(pandas, "api"): # pandas >= 0.19
_pandas_is_categorical_dtype = getattr(pandas.api.types, "is_categorical_dtype", None)
# pandas.api.types._pandas_is_categorical_dtype is deprecated in pandas v2.1.0
if _pandas_is_categorical_dtype is not None:
import warnings
with warnings.catch_warnings(record=True) as w:
_pandas_is_categorical_dtype(int)
if len(w) > 0 and issubclass(w[-1].category, FutureWarning):
_pandas_is_categorical_dtype = None
else:
_pandas_is_categorical_dtype = lambda x: isinstance(x, pandas.CategoricalDType)
else:
# This is needed for pandas v0.18.0 and earlier
else: # pandas <=0.18
_pandas_is_categorical_dtype = getattr(pandas.core.common,
"is_categorical_dtype", None)
have_pandas_categorical_dtype = (_pandas_is_categorical_dtype
is not None)
have_pandas_categorical_dtype = _pandas_is_categorical_dtype is not None


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

0 comments on commit 37a0388

Please sign in to comment.