Skip to content

Commit

Permalink
Avoid deprecated pandas is_categorical_dtype API
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Jan 4, 2024
1 parent ae3e8cb commit 7409bc6
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions patsy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,13 @@
_pandas_is_categorical_dtype = None
else:
if hasattr(pandas, "api"):
# This is available starting in pandas v0.19.0
# The pandas `api` submodule debuted with pandas v0.19. The helper
# method `is_categorical_dtype` exists as supported API prior to pandas
# 2.1.0, but internally it was equivalent to `isinstance(x,
# pandas.CategoricalDType)` for our use-cases. Since this is now the
# preferred code pathway, we use it always.
have_pandas_categorical_dtype = True
_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)
_pandas_is_categorical_dtype = lambda x: isinstance(x, pandas.CategoricalDType)
else:
# This is needed for pandas v0.18.0 and earlier
_pandas_is_categorical_dtype = getattr(pandas.core.common,
Expand Down

0 comments on commit 7409bc6

Please sign in to comment.