Skip to content

Support pandas nullable dtypes in safe_issubdtype - #282

Open
RaphaelFakhri wants to merge 1 commit into
pydata:masterfrom
RaphaelFakhri:fix-pandas-nullable-dtypes
Open

RaphaelFakhri wants to merge 1 commit into
pydata:masterfrom
RaphaelFakhri:fix-pandas-nullable-dtypes

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

safe_issubdtype passes pandas extension dtypes such as BooleanDtype, Int64Dtype and pyarrow bool[pyarrow] to np.issubdtype, which raises TypeError: Cannot interpret 'BooleanDtype' as a data type. Because the categorical sniffer and categorical_to_int call it on the raw Series dtype, dmatrix("b", df) on a nullable boolean column and dmatrix("C(i)", df) on a nullable integer column fail, even without missing values. DataFrame.convert_dtypes() produces these dtypes.

This change uses the dtype's numpy_dtype attribute when it exists, before the call to np.issubdtype. Plain numpy dtypes don't have this attribute, so their behavior doesn't change.

Tests

  • test_safe_issubdtype covers BooleanDtype and Int64Dtype.
  • test_pandas_nullable_dtypes builds design matrices from nullable boolean and integer columns.

Both tests fail without the fix and pass with it. The full suite passes with numpy 2.5.4 and pandas 3.1.0, with numpy 1.26.4 and pandas 1.5.3, and without pandas.

Columns that contain pd.NA are out of scope. patsy doesn't yet treat pd.NA as missing for any dtype.

Fixes #281

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant