Skip to content

Commit 6c73617

Browse files
committed
Adapt to new location of pandas is_categorical_dtype
I'm not sure what version this changed in, but originally the only way to get is_categorical_dtype was as a private export from pandas.core.common. Now that's deprecated, and you're supposed to get it from pandas.api.type. We try both, to cover both periods.
1 parent bf503bb commit 6c73617

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ matrix:
1212
env: PANDAS_VERSION_STR="=0.14.0 libgfortran=1.0"
1313
- python: 2.7
1414
env: PANDAS_VERSION_STR="=0.14.0 libgfortran=1.0"
15+
# 0.18.0 has is_categorical_dtype in a different place than 0.19.0+
16+
- python: 3.4
17+
env: PANDAS_VERSION_STR="=0.18.0"
18+
- python: 2.7
19+
env: PANDAS_VERSION_STR="=0.18.0"
20+
1521
# This disables sudo, but makes builds start much faster
1622
# See http://blog.travis-ci.com/2014-12-17-faster-builds-with-container-based-infrastructure/
1723
sudo: false

patsy/util.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@
4040
# Can drop this guard whenever we drop support for such older versions of
4141
# pandas.
4242
have_pandas_categorical = (have_pandas and hasattr(pandas, "Categorical"))
43-
have_pandas_categorical_dtype = (have_pandas
44-
and hasattr(pandas.core.common,
45-
"is_categorical_dtype"))
43+
if not have_pandas:
44+
have_pandas_categorical_dtype = False
45+
_pandas_is_categorical_dtype = None
46+
else:
47+
if hasattr(pandas, "api"):
48+
# This is available starting in pandas v0.19.0
49+
have_pandas_categorical_dtype = True
50+
_pandas_is_categorical_dtype = pandas.api.types.is_categorical_dtype
51+
else:
52+
# This is needed for pandas v0.18.0 and earlier
53+
_pandas_is_categorical_dtype = getattr(pandas.core.common,
54+
"is_categorical_dtype", None)
55+
have_pandas_categorical_dtype = (_pandas_is_categorical_dtype
56+
is not None)
4657

4758
# Passes through Series and DataFrames, call np.asarray() on everything else
4859
def asarray_or_pandas(a, copy=False, dtype=None, subok=False):
@@ -637,10 +648,7 @@ def test_pandas_Categorical_accessors():
637648
def safe_is_pandas_categorical_dtype(dt):
638649
if not have_pandas_categorical_dtype:
639650
return False
640-
# WTF this incredibly crucial function is not even publically exported.
641-
# Also if you read its source it uses a bare except: block which is broken
642-
# by definition, but oh well there is not much I can do about this.
643-
return pandas.core.common.is_categorical_dtype(dt)
651+
return _pandas_is_categorical_dtype(dt)
644652

645653
# Needed to support pandas >= 0.15 (!)
646654
def safe_is_pandas_categorical(data):

0 commit comments

Comments
 (0)