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

Contingent validation & optional columns #31

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas_schema/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from .validation_warning import ValidationWarning

class Column:
def __init__(self, name: str, validations: typing.Iterable['validation._BaseValidation'] = [], allow_empty=False):
def __init__(self, name: str, validations: typing.Iterable['validation._BaseValidation'] = [],
allow_empty=False,
optional=False):
"""
Creates a new Column object

Expand All @@ -16,6 +18,7 @@ def __init__(self, name: str, validations: typing.Iterable['validation._BaseVali
self.name = name
self.validations = list(validations)
self.allow_empty = allow_empty
self.optional = optional

def validate(self, series: pd.Series) -> typing.List[ValidationWarning]:
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas_schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def validate(self, df: pd.DataFrame, columns: typing.List[str] = None) -> typing
for column in columns_to_pair:

# Throw an error if the schema column isn't in the data frame
if column.name not in df:
if column.name not in df and not column.optional:
errors.append(ValidationWarning(
'The column {} exists in the schema but not in the data frame'.format(column.name)))
return errors
Expand Down
3 changes: 2 additions & 1 deletion pandas_schema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def get_errors(self, series: pd.Series, column: 'column.Column'):
validated = ~series.isnull() & simple_validation
else:
validated = (series.str.len() > 0) & simple_validation

elif column.optional and (bool(series.isnull().all()) or list(series.unique()) == ['']):
validated = []
else:
validated = simple_validation

Expand Down
12 changes: 12 additions & 0 deletions test/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,18 @@ def test_in_range_allow_empty_false_with_error(self):
errors = validator.get_errors(pd.Series(self.vals), Column('', allow_empty=False))
self.assertEqual(len(errors), len(self.vals))

def test_in_range_optional_missing(self):
validator = InRangeValidation(min=0)
errors = validator.get_errors(pd.Series(), Column('', optional=True))

self.assertEqual(len(errors), 0)

def test_in_range_optional_with_error(self):
validator = InRangeValidation(min=4)
errors = validator.get_errors(pd.Series(self.vals), Column('', optional=False))

self.assertEqual(len(errors), len(self.vals))


class PandasDtypeTests(ValidationTestBase):
"""
Expand Down