Repository navigation
Support itemsize-parameterized numpy dtypes in the pandas engine - #2437
Merged
cosmicbboy merged 1 commit intoAug 19, 2026
Merged
cosmicbboy merged 1 commit into
cosmicbboy merged 1 commit into
Conversation
2 of 3 tasks
Declaring a column with a sized numpy dtype raised a confusing TypeError
that named a dtype the user never wrote:
pa.DataFrameSchema({"a": pa.Column(np.dtype("S16"))})
TypeError: data type 'bytes128' not understood
Engine.dtype normalizes platform-specific aliases by round-tripping the
dtype through its name, so that e.g. np.intc becomes np.int32. That works
for fixed-width numeric dtypes, whose name is itself a valid dtype string,
but not for itemsize-parameterized ones: np.dtype("S16").name is
"bytes128", np.dtype("U16").name is "str512", and neither parses back.
Fall back to the dtype itself when the name does not round-trip. Its
scalar type is already platform-agnostic, so the subsequent .type lookup
resolves as it does for the unsized spelling: np.dtype("S16") now resolves
the same as bytes, and np.dtype("U16") the same as np.str_.
Note that using np_or_pd_dtype.type directly instead of the round-trip
would be simpler but silently regress the normalization this code exists
for -- np.dtype("longlong").type is np.longlong, not np.int64 -- so the
round-trip is kept and only guarded. Tests cover both directions.
Fixes unionai-oss#1270
Signed-off-by: Karthik Mannem <180236770+mannemkarthik010@users.noreply.github.com>
mannemkarthik010
force-pushed
the
fix-itemsize-parameterized-numpy-dtypes
branch
from
August 7, 2026 16:49
771b37e to
43b8fe5
Compare
cosmicbboy
approved these changes
Aug 19, 2026
barlowa124
pushed a commit
to barlowa124/pandera
that referenced
this pull request
Dec 4, 2026
…onai-oss#2437) Declaring a column with a sized numpy dtype raised a confusing TypeError that named a dtype the user never wrote: pa.DataFrameSchema({"a": pa.Column(np.dtype("S16"))}) TypeError: data type 'bytes128' not understood Engine.dtype normalizes platform-specific aliases by round-tripping the dtype through its name, so that e.g. np.intc becomes np.int32. That works for fixed-width numeric dtypes, whose name is itself a valid dtype string, but not for itemsize-parameterized ones: np.dtype("S16").name is "bytes128", np.dtype("U16").name is "str512", and neither parses back. Fall back to the dtype itself when the name does not round-trip. Its scalar type is already platform-agnostic, so the subsequent .type lookup resolves as it does for the unsized spelling: np.dtype("S16") now resolves the same as bytes, and np.dtype("U16") the same as np.str_. Note that using np_or_pd_dtype.type directly instead of the round-trip would be simpler but silently regress the normalization this code exists for -- np.dtype("longlong").type is np.longlong, not np.int64 -- so the round-trip is kept and only guarded. Tests cover both directions. Fixes unionai-oss#1270 Signed-off-by: Karthik Mannem <180236770+mannemkarthik010@users.noreply.github.com>
barlowa124
pushed a commit
to barlowa124/pandera
that referenced
this pull request
Dec 4, 2026
…onai-oss#2437) Declaring a column with a sized numpy dtype raised a confusing TypeError that named a dtype the user never wrote: pa.DataFrameSchema({"a": pa.Column(np.dtype("S16"))}) TypeError: data type 'bytes128' not understood Engine.dtype normalizes platform-specific aliases by round-tripping the dtype through its name, so that e.g. np.intc becomes np.int32. That works for fixed-width numeric dtypes, whose name is itself a valid dtype string, but not for itemsize-parameterized ones: np.dtype("S16").name is "bytes128", np.dtype("U16").name is "str512", and neither parses back. Fall back to the dtype itself when the name does not round-trip. Its scalar type is already platform-agnostic, so the subsequent .type lookup resolves as it does for the unsized spelling: np.dtype("S16") now resolves the same as bytes, and np.dtype("U16") the same as np.str_. Note that using np_or_pd_dtype.type directly instead of the round-trip would be simpler but silently regress the normalization this code exists for -- np.dtype("longlong").type is np.longlong, not np.int64 -- so the round-trip is kept and only guarded. Tests cover both directions. Fixes unionai-oss#1270 Signed-off-by: Karthik Mannem <180236770+mannemkarthik010@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1270
Declaring a column with a sized numpy dtype raised a
TypeErrornaming a dtype the user never wrote:Engine.dtypenormalizes platform-specific aliases by round-tripping the dtype through itsname, so that e.g.np.intcbecomesnp.int32. That works for fixed-width numeric dtypes, whosenameis itself a valid dtype string, but not for itemsize-parameterized ones:np.dtype("S16").nameis"bytes128"andnp.dtype("U16").nameis"str512"-- neither parses back.This falls back to the dtype itself when the name does not round-trip. Its scalar type is already platform-agnostic, so the subsequent
.typelookup resolves as it does for the unsized spelling:np.dtype("S16")now resolves the same asbytes, andnp.dtype("U16")the same asnp.str_.Worth noting: using
np_or_pd_dtype.typedirectly instead of the round-trip would be simpler, but it silently regresses the normalization this code exists for --np.dtype("longlong").typeisnp.longlong, notnp.int64. So the round-trip is kept and only guarded.The issue reports this for bytes; it affects sized unicode (
U16) the same way. Void (V8) remains unsupported by the engine, but now surfaces pandera's own "not understood by Engine" error rather than a numpy parse error.Test plan
Added to
tests/pandas/test_pandas_engine.py:test_itemsize_parameterized_numpy_dtype--S16/S1/U16/U100resolve like their scalar typetest_itemsize_parameterized_numpy_dtype_validates-- a schema declared withnp.dtype("S16")validates real datatest_platform_alias_numpy_dtype_still_normalized--intc/uintc/longlong/ulonglongstill normalize (guards the regression described above)5 of these fail on
mainand all pass with this change. The fulltest_pandas_engine.py,test_numpy_engine.py,test_engine.pyandtest_dtypes.pysuites pass (887 tests).ruff check,ruff formatandcodespellare clean.