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_empty support for datetime types #44

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions pandas_schema/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from . import column
from .validation_warning import ValidationWarning
from .errors import PanSchArgumentError
from pandas.api.types import is_categorical_dtype, is_numeric_dtype
from pandas.api.types import is_categorical_dtype, is_datetime64_any_dtype, is_numeric_dtype


class _BaseValidation:
Expand Down Expand Up @@ -85,8 +85,8 @@ def get_errors(self, series: pd.Series, column: 'column.Column'):
simple_validation = ~self.validate(series)
if column.allow_empty:
# Failing results are those that are not empty, and fail the validation
# explicitly check to make sure the series isn't a category because issubdtype will FAIL if it is
if is_categorical_dtype(series) or is_numeric_dtype(series):
# explicitly check to make sure the series isn't a category/datetime because issubdtype will FAIL if it is
if is_categorical_dtype(series) or is_datetime64_any_dtype(series) or is_numeric_dtype(series):
validated = ~series.isnull() & simple_validation
else:
validated = (series.str.len() > 0) & simple_validation
Expand Down
24 changes: 24 additions & 0 deletions test/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,27 @@ def test_invalid_elements(self):
errors = self.validator.get_errors(pd.Series(['aa', 'bb', 'd'], dtype='category'),
Column('', allow_empty=True))
self.assertEqual(len(errors), 3)

class GetErrorAllowEmptyDatetimeTests(ValidationTestBase):
"""
Tests for datetime valued columns where allow_empty=True
"""

def setUp(self):
match_val = datetime.datetime(2020, 11, 1)
self.validator = CustomSeriesValidation(lambda s: s == match_val, 'did not match target date')

def test_valid(self):
series = pd.Series(['2020-11-01'], dtype='datetime64[ns]')
errors = self.validator.get_errors(series, Column('', allow_empty=True))
self.assertEqual(len(errors), 0)

def test_valid_invalid(self):
series = pd.Series(['2020-11-01', '2025-01-01'], dtype='datetime64[ns]')
errors = self.validator.get_errors(series, Column('', allow_empty=True))
self.assertEqual(len(errors), 1)

def test_valid_invalid_empty(self):
series = pd.Series(['2020-11-01', '2025-01-01', pd.NaT, np.NaN], dtype='datetime64[ns]')
errors = self.validator.get_errors(series, Column('', allow_empty=True))
self.assertEqual(len(errors), 1)