Skip to content

Commit 8eff8dd

Browse files
committed
fix tests, coverage
1 parent 49471ba commit 8eff8dd

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

pandera/engines/pandas_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class BOOL(DataType, dtypes.Bool):
198198
"""Semantic representation of a :class:`pandas.BooleanDtype`."""
199199

200200
type = pd.BooleanDtype()
201-
_bool_like = frozenset({1, 0, 1.0, 0.0, True, False})
201+
_bool_like = frozenset({True, False})
202202

203203
def coerce_value(self, value: Any) -> Any:
204204
"""Coerce an value to specified datatime type."""
@@ -440,7 +440,7 @@ def coerce(self, data_container: PandasObject) -> PandasObject:
440440

441441
def coerce_value(self, value: Any) -> Any:
442442
"""Coerce an value to a particular type."""
443-
if value not in self.type.categories: # pylint: disable=no-member
443+
if value not in self.categories: # type: ignore
444444
raise TypeError(
445445
f"value {value} cannot be coerced to type {self.type}"
446446
)

tests/core/test_pandas_engine.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Test numpy engine."""
22

3+
import hypothesis.strategies as st
34
import pandas as pd
45
import pytest
6+
from hypothesis import given
57

68
from pandera.engines import pandas_engine
79
from pandera.errors import ParserError
@@ -42,3 +44,65 @@ def test_pandas_data_type_coerce(data_type):
4244
data_type().try_coerce(pd.Series(["1", "2", "a"]))
4345
except ParserError as exc:
4446
assert exc.failure_cases.shape[0] > 0
47+
48+
49+
CATEGORIES = ["A", "B", "C"]
50+
51+
52+
@given(st.lists(st.sampled_from(CATEGORIES), min_size=5))
53+
def test_pandas_category_dtype(data):
54+
"""Test pandas_engine.Category correctly coerces valid categorical data."""
55+
data = pd.Series(data)
56+
dtype = pandas_engine.Category(CATEGORIES)
57+
coerced_data = dtype.coerce(data)
58+
assert dtype.check(coerced_data.dtype)
59+
60+
for _, value in data.iteritems():
61+
coerced_value = dtype.coerce_value(value)
62+
assert coerced_value in CATEGORIES
63+
64+
65+
@given(st.lists(st.sampled_from(["X", "Y", "Z"]), min_size=5))
66+
def test_pandas_category_dtype_error(data):
67+
"""Test pandas_engine.Category raises TypeErrors on invalid data."""
68+
data = pd.Series(data)
69+
dtype = pandas_engine.Category(CATEGORIES)
70+
71+
with pytest.raises(TypeError):
72+
dtype.coerce(data)
73+
74+
for _, value in data.iteritems():
75+
with pytest.raises(TypeError):
76+
dtype.coerce_value(value)
77+
78+
79+
@given(st.lists(st.sampled_from([1, 0, 1.0, 0.0, True, False]), min_size=5))
80+
def test_pandas_boolean_native_type(data):
81+
"""Test native pandas bool type correctly coerces valid bool-like data."""
82+
data = pd.Series(data)
83+
dtype = pandas_engine.Engine.dtype("boolean")
84+
85+
# the BooleanDtype can't handle Series of non-boolean, mixed dtypes
86+
if data.dtype == "object":
87+
with pytest.raises(TypeError):
88+
dtype.coerce(data)
89+
else:
90+
coerced_data = dtype.coerce(data)
91+
assert dtype.check(coerced_data.dtype)
92+
93+
for _, value in data.iteritems():
94+
dtype.coerce_value(value)
95+
96+
97+
@given(st.lists(st.sampled_from(["A", "True", "False", 5, -1]), min_size=5))
98+
def test_pandas_boolean_native_type_error(data):
99+
"""Test native pandas bool type raises TypeErrors on non-bool-like data."""
100+
data = pd.Series(data)
101+
dtype = pandas_engine.Engine.dtype("boolean")
102+
103+
with pytest.raises(TypeError):
104+
dtype.coerce(data)
105+
106+
for _, value in data.iteritems():
107+
with pytest.raises(TypeError):
108+
dtype.coerce_value(value)

0 commit comments

Comments
 (0)