Skip to content

Commit 20749a1

Browse files
authored
Merge pull request #96 from njsmith/pandas-is-categorical-dtype-moved
Adapt to changes in pandas 0.19.0
2 parents bf503bb + a83ff74 commit 20749a1

5 files changed

Lines changed: 33 additions & 19 deletions

File tree

.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/categorical.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def guess_categorical(data):
134134

135135
def test_guess_categorical():
136136
if have_pandas_categorical:
137-
c = pandas.Categorical.from_array([1, 2, 3])
137+
c = pandas.Categorical([1, 2, 3])
138138
assert guess_categorical(c)
139139
if have_pandas_categorical_dtype:
140140
assert guess_categorical(pandas.Series(c))
@@ -242,15 +242,15 @@ def t(NA_types, datas, exp_finish_fast, exp_levels, exp_contrast=None):
242242
preps += [pandas.Series,
243243
lambda x: C(pandas.Series(x))]
244244
for prep in preps:
245-
t([], [prep(pandas.Categorical.from_array([1, 2, None]))],
245+
t([], [prep(pandas.Categorical([1, 2, None]))],
246246
True, (1, 2))
247247
# check order preservation
248248
t([], [prep(pandas_Categorical_from_codes([1, 0], ["a", "b"]))],
249249
True, ("a", "b"))
250250
t([], [prep(pandas_Categorical_from_codes([1, 0], ["b", "a"]))],
251251
True, ("b", "a"))
252252
# check that if someone sticks a .contrast field onto our object
253-
obj = prep(pandas.Categorical.from_array(["a", "b"]))
253+
obj = prep(pandas.Categorical(["a", "b"]))
254254
obj.contrast = "CONTRAST"
255255
t([], [obj], True, ("a", "b"), "CONTRAST")
256256

patsy/test_build.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def __getitem__(self, key):
429429
return_type="dataframe")
430430
assert isinstance(int_df, pandas.DataFrame)
431431
assert np.array_equal(int_df, [[1], [1], [1]])
432-
assert int_df.index.equals([10, 20, 30])
432+
assert int_df.index.equals(pandas.Index([10, 20, 30]))
433433

434434
import patsy.build
435435
had_pandas = patsy.build.have_pandas
@@ -448,7 +448,7 @@ def __getitem__(self, key):
448448
dtype=object)},
449449
NA_action="drop",
450450
return_type="dataframe")
451-
assert x_df.index.equals([2])
451+
assert x_df.index.equals(pandas.Index([2]))
452452

453453
def test_data_mismatch():
454454
test_cases_twoway = [
@@ -580,7 +580,7 @@ def test_categorical():
580580
data_categ = {"a": C(["a2", "a1", "a2"])}
581581
datas = [data_strings, data_categ]
582582
if have_pandas_categorical:
583-
data_pandas = {"a": pandas.Categorical.from_array(["a1", "a2", "a2"])}
583+
data_pandas = {"a": pandas.Categorical(["a1", "a2", "a2"])}
584584
datas.append(data_pandas)
585585
def t(data1, data2):
586586
def iter_maker():

patsy/test_highlevel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def test_dmatrix_NA_action():
672672
assert np.array_equal(mat, [[1, 2, 20],
673673
[1, 3, 30]])
674674
if return_type == "dataframe":
675-
assert mat.index.equals([1, 2])
675+
assert mat.index.equals(pandas.Index([1, 2]))
676676
assert_raises(PatsyError, dmatrix, "x + y", data=data,
677677
return_type=return_type,
678678
NA_action="raise")
@@ -681,8 +681,8 @@ def test_dmatrix_NA_action():
681681
assert np.array_equal(lmat, [[20], [30]])
682682
assert np.array_equal(rmat, [[1, 2], [1, 3]])
683683
if return_type == "dataframe":
684-
assert lmat.index.equals([1, 2])
685-
assert rmat.index.equals([1, 2])
684+
assert lmat.index.equals(pandas.Index([1, 2]))
685+
assert rmat.index.equals(pandas.Index([1, 2]))
686686
assert_raises(PatsyError,
687687
dmatrices, "y ~ x", data=data, return_type=return_type,
688688
NA_action="raise")
@@ -693,8 +693,8 @@ def test_dmatrix_NA_action():
693693
assert np.array_equal(lmat, [[20], [30], [40]])
694694
assert np.array_equal(rmat, [[1], [1], [1]])
695695
if return_type == "dataframe":
696-
assert lmat.index.equals([1, 2, 3])
697-
assert rmat.index.equals([1, 2, 3])
696+
assert lmat.index.equals(pandas.Index([1, 2, 3]))
697+
assert rmat.index.equals(pandas.Index([1, 2, 3]))
698698
assert_raises(PatsyError,
699699
dmatrices, "y ~ 1", data=data, return_type=return_type,
700700
NA_action="raise")

patsy/util.py

Lines changed: 16 additions & 8 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):
@@ -656,7 +664,7 @@ def test_safe_is_pandas_categorical():
656664
assert not safe_is_pandas_categorical(np.arange(10))
657665

658666
if have_pandas_categorical:
659-
c_obj = pandas.Categorical.from_array(["a", "b"])
667+
c_obj = pandas.Categorical(["a", "b"])
660668
assert safe_is_pandas_categorical(c_obj)
661669

662670
if have_pandas_categorical_dtype:

0 commit comments

Comments
 (0)