|
19 | 19 | build_design_matrices, |
20 | 20 | DesignMatrixBuilder) |
21 | 21 | 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) |
23 | 26 | from patsy.origin import Origin |
24 | 27 |
|
25 | 28 | if have_pandas: |
@@ -702,3 +705,34 @@ def test_0d_data(): |
702 | 705 | assert np.allclose(build_design_matrices([mat.design_info.builder], |
703 | 706 | data_series)[0], |
704 | 707 | 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