Skip to content

Commit 756da02

Browse files
committed
Add a high level test of interaction between C() and pandas Categoricals
1 parent 49b390b commit 756da02

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

patsy/test_highlevel.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
build_design_matrices,
2020
DesignMatrixBuilder)
2121
from patsy.highlevel import *
22-
from patsy.util import have_pandas
22+
from patsy.util import (have_pandas,
23+
have_pandas_categorical,
24+
have_pandas_categorical_dtype,
25+
pandas_Categorical_from_codes)
2326
from patsy.origin import Origin
2427

2528
if have_pandas:
@@ -702,3 +705,34 @@ def test_0d_data():
702705
assert np.allclose(build_design_matrices([mat.design_info.builder],
703706
data_series)[0],
704707
expected)
708+
709+
def test_C_and_pandas_categorical():
710+
if not have_pandas_categorical:
711+
return
712+
713+
objs = [pandas_Categorical_from_codes([1, 0, 1], ["b", "a"])]
714+
if have_pandas_categorical_dtype:
715+
objs.append(pandas.Series(objs[0]))
716+
for obj in objs:
717+
d = {"obj": obj}
718+
assert np.allclose(dmatrix("obj", d),
719+
[[1, 1],
720+
[1, 0],
721+
[1, 1]])
722+
723+
assert np.allclose(dmatrix("C(obj)", d),
724+
[[1, 1],
725+
[1, 0],
726+
[1, 1]])
727+
728+
assert np.allclose(dmatrix("C(obj, levels=['b', 'a'])", d),
729+
[[1, 1],
730+
[1, 0],
731+
[1, 1]])
732+
733+
assert np.allclose(dmatrix("C(obj, levels=['a', 'b'])", d),
734+
[[1, 0],
735+
[1, 1],
736+
[1, 0]])
737+
738+
# From https://github.com/pydata/patsy/pull/47#issuecomment-72605624

0 commit comments

Comments
 (0)