You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nullable pandas dtypes crash in categorical handling: "Cannot interpret 'BooleanDtype' as a data type" #281
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
importpandasaspdimportpatsydf=pd.DataFrame({"b": [True, False, True], "i": [1, 2, 1]}).convert_dtypes()
patsy.dmatrix("b", df) # TypeError: Cannot interpret 'BooleanDtype' as a data typepatsy.dmatrix("C(i)", df) # TypeError: Cannot interpret 'Int64Dtype()' as a data typedf=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).
Activity