Skip to content

Nullable pandas dtypes crash in categorical handling: "Cannot interpret 'BooleanDtype' as a data type" #281

Open

Description

@RaphaelFakhri

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).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions