Nullable pandas dtypes crash in categorical handling: "Cannot interpret 'BooleanDtype' as a data type"
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start by reading CategoricalSniffer.sniff() and categorical_to_int() in the Patsy source, then inspect the existing tests for categorical handling. Reproduce the failures with pandas nullable and pyarrow dtypes, including Boolean and integer columns. Done means categorical handling works for the shown dtypes; the separate pd.NA missing-value gap is also identified in the issue.
Written by the indexing model from the issue text.
Description
A DataFrame column with a pandas nullable or pyarrow dtype raises TypeError when patsy treats it as categorical, even when the column has no missing values. DataFrame.convert_dtypes() and read_csv(dtype_backend=...) produce these dtypes.
Reproduce
import pandas as pd
import patsy
df = pd.DataFrame({"b": [True, False, True], "i": [1, 2, 1]}).convert_dtypes()
patsy.dmatrix("b", df) # TypeError: Cannot interpret 'BooleanDtype' as a data type
patsy.dmatrix("C(i)", df) # TypeError: Cannot interpret 'Int64Dtype()' as a data type
df = df.convert_dtypes(dtype_backend="pyarrow")
patsy.dmatrix("b", df) # TypeError: Cannot interpret 'bool[pyarrow]' as a data type
Tested with patsy main (38a1ac5), numpy 2.5.4 and pandas 3.1.0. Numeric use of the same columns (dmatrix("i", df)) works.
Cause
CategoricalSniffer.sniff() and categorical_to_int() call safe_issubdtype(data.dtype, np.bool_) on the pandas Series. safe_issubdtype passes the pandas extension dtype to np.issubdtype, and numpy can't interpret it. Nullable and pyarrow dtypes expose the equivalent numpy dtype as numpy_dtype.
Columns that contain pd.NA are a separate gap: patsy doesn't treat pd.NA as missing in any dtype (string, Int64, boolean).
- Dominant language
- Python
- Stars
- 990
- Forks
- 113
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 3
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydata/patsy
-
VarLookupDict should implement __iter__, causes CPython crash when using statsmodels in marimo notebookPossibly taken @kouyouqi123 claimed this 173 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Add link to license in readmePossibly taken @zheng-guannan577 claimed this 9 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
cc() truncates periodic coordinates for integer input with fractional boundsPossibly taken @ktz03 claimed this 39 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
-
`build._eval_factor` error condition attempts to access attribute `dtype` on `DataFrame` object, but no such attribute existsPossibly taken @kouyouqi123 claimed this 173 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
abetlen/llama-cpp-python#2424 ·
Maintainers usually reply within 1 day
-
Bug IronPython Python 3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pyrevitlabs/pyRevit#4184 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day