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

allow InListValidation to ignore missings #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions pandas_schema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,14 @@ class InListValidation(_SeriesValidation):
Checks that each element in this column is contained within a list of possibilities
"""

def __init__(self, options: typing.Iterable, case_sensitive: bool = True, **kwargs):
def __init__(self, options: typing.Iterable, case_sensitive: bool = True, ignore_nas: bool = False, **kwargs):
"""
:param options: A list of values to check. If the value of a cell is in this list, it is considered to pass the
validation
:param ignore_nas: ignore nas (boolean, default False)
"""
self.case_sensitive = case_sensitive
self.ignore_nan = ignore_nan
self.options = options
super().__init__(**kwargs)

Expand All @@ -380,9 +382,14 @@ def default_message(self):

def validate(self, series: pd.Series) -> pd.Series:
if self.case_sensitive:
return series.isin(self.options)
is_in_options = series.isin(self.options)
else:
return series.str.lower().isin([s.lower() for s in self.options])
is_in_options = series.str.lower().isin([s.lower() for s in self.options])

if self.ignore_nas:
is_in_options = is_in_options | series.isna()

return is_in_options


class DateFormatValidation(_SeriesValidation):
Expand Down
38 changes: 38 additions & 0 deletions test/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,44 @@ def test_invalid_elements(self):
'accepts elements that are not in the validation list'
)

class InListIgnoringNas(ValidationTestBase):
def setUp(self):
self.validator = InListValidation(['a', 'b', 'c'], ignore_nas=True)

def test_valid_elements(self):
self.validate_and_compare(
[
np.NaN
],
True,
'does not ignore NaNs when it should'
)

class InListNotIgnoringNas(ValidationTestBase):
def setUp(self):
self.validator = InListValidation(['a', 'b', 'c'], ignore_nas=False)

def test_valid_elements(self):
self.validate_and_compare(
[
np.NaN
],
False,
'ignores NaNs when it should not'
)

class InListIgnoringNas(ValidationTestBase):
def setUp(self):
self.validator = InListValidation(['a', 'b', 'c'], ignore_nas=True)

def test_valid_elements(self):
self.validate_and_compare(
[
np.NaN
],
True,
'does not ignore NaNs'
)

class DateFormat(ValidationTestBase):
def setUp(self):
Expand Down