Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remaining unrecognized numpy dtypes #637

Merged
merged 4 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pandera/engines/numpy_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ class Complex64(Complex128):
bit_width: int = 64


###############################################################################
# bytes
###############################################################################


@Engine.register_dtype(equivalents=["bytes", bytes, np.bytes_])
@immutable
class Bytes(DataType):
type = np.dtype("bytes")


###############################################################################
# string
###############################################################################
Expand Down
10 changes: 9 additions & 1 deletion pandera/engines/pandas_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ def dtype(cls, data_type: Any) -> "DataType":
# into a numpy or pandas dtype.
np_or_pd_dtype = pd.api.types.pandas_dtype(data_type)
if isinstance(np_or_pd_dtype, np.dtype):
np_or_pd_dtype = np_or_pd_dtype.type
# cast alias to platform-agnostic dtype
# e.g.: np.intc -> np.int32
common_np_dtype = np.dtype(np_or_pd_dtype.name)
np_or_pd_dtype = common_np_dtype.type

return engine.Engine.dtype(cls, np_or_pd_dtype)

Expand Down Expand Up @@ -454,13 +457,18 @@ def check(self, pandera_dtype: dtypes.DataType) -> bool:
numpy_engine.Object,
equivalents=[
"object",
"object_",
"object0",
"O",
"bytes",
"decimal",
"mixed-integer",
"mixed",
"bytes",
bytes,
object,
np.object_,
np.bytes_,
],
)

Expand Down
22 changes: 22 additions & 0 deletions tests/core/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,28 @@ def test_default_numeric_dtypes():
)


@pytest.mark.parametrize(
"alias, np_dtype",
[
(alias, np_dtype)
for alias, np_dtype in np.sctypeDict.items()
# int, uint have different bitwidth under pandas and numpy.
if np_dtype != np.void and alias not in ("int", "uint")
],
)
def test_numpy_dtypes(alias, np_dtype):
"""Test that all pandas-compatible numpy dtypes are understood."""
try:
np.dtype(alias)
except TypeError:
# not a valid alias
assert pandas_engine.Engine.dtype(np_dtype)
else:
assert pandas_engine.Engine.dtype(alias) == pandas_engine.Engine.dtype(
np_dtype
)


@pytest.mark.parametrize(
"examples",
[
Expand Down