-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test and PANDERA_VALIDATION_ENABLED support for both pandas serie…
…s and dataframe Signed-off-by: Nok <[email protected]>
- Loading branch information
Showing
5 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. currentmodule:: pandera | ||
|
||
.. _configuration: | ||
|
||
Configuration | ||
=============== | ||
|
||
*New in version 0.17.3* | ||
``pandera`` provides a global config `~pandera.config.PanderaConfig`. | ||
|
||
This configuration can also be set using environment variables. For instance: | ||
``` | ||
export PANDERA_VALIDATION_ENABLED=False | ||
export PANDERA_VALIDATION_DEPTH=DATA_ONLY | ||
``` | ||
|
||
Runtime data validation incurs a performance overhead. To mitigate this, you have the option to disable validation globally. This can be achieved by setting the environment variable `PANDERA_VALIDATION_ENABLE=False`. When validation is disabled, any `validate` call will return `None`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""This module is to test the behaviour change based on defined config in pandera""" | ||
# pylint:disable=import-outside-toplevel,abstract-method | ||
|
||
|
||
import pandera as pa | ||
from pandera.config import CONFIG, ValidationDepth | ||
import pandas as pd | ||
from pandera import DataFrameModel, DataFrameSchema, SeriesSchema | ||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def disable_validation(): | ||
CONFIG.validation_enabled = False | ||
yield "resource" | ||
CONFIG.validation_enabled = True | ||
|
||
|
||
class TestPandasDataFrameConfig: | ||
"""Class to test all the different configs types""" | ||
|
||
sample_data = pd.DataFrame( | ||
(("Bread", 9), ("Cutter", 15)), columns=["product", "price_val"] | ||
) | ||
|
||
def test_disable_validation(self, disable_validation): | ||
"""This function validates that a none object is loaded if validation is disabled""" | ||
|
||
pandera_schema = DataFrameSchema( | ||
{ | ||
"product": pa.Column( | ||
str, pa.Check(lambda s: s.str.startswith("B")) | ||
), | ||
"price_val": pa.Column(int), | ||
} | ||
) | ||
|
||
class TestSchema(DataFrameModel): | ||
"""Test Schema class""" | ||
|
||
product: str = pa.Field(str_startswith="B") | ||
price_val: int = pa.Field() | ||
|
||
expected = { | ||
"validation_enabled": False, | ||
"validation_depth": ValidationDepth.SCHEMA_AND_DATA, | ||
} | ||
|
||
assert CONFIG.dict() == expected | ||
assert pandera_schema.validate(self.sample_data) is self.sample_data | ||
assert TestSchema.validate(self.sample_data) is self.sample_data | ||
|
||
|
||
class TestPandasSeriesConfig: | ||
"""Class to test all the different configs types""" | ||
|
||
sample_data = pd.Series([1, 1, 2, 2, 3, 3]) | ||
|
||
def test_disable_validation(self, disable_validation): | ||
"""This function validates that a none object is loaded if validation is disabled""" | ||
expected = { | ||
"validation_enabled": False, | ||
"validation_depth": ValidationDepth.SCHEMA_AND_DATA, | ||
} | ||
pandera_schema = SeriesSchema( | ||
int, pa.Check(lambda s: s.value_counts() == 2, element_wise=False) | ||
) | ||
assert CONFIG.dict() == expected | ||
assert pandera_schema.validate(self.sample_data) is self.sample_data |