-
-
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 support for
PANDERA_VALIDATION_ENABLED
for pandas and Configura…
…tion docs (#1354) * Add PANDERA_VALIDATION_ENABLED to pandas container Signed-off-by: Nok Lam Chan <[email protected]> Signed-off-by: Nok <[email protected]> * Placeholder Signed-off-by: Nok Lam Chan <[email protected]> Signed-off-by: Nok <[email protected]> * Add support for PANDERA_VALIDATION_ENABLED for pandas Signed-off-by: Nok <[email protected]> * Please linting & DCO Signed-off-by: Nok <[email protected]> --------- Signed-off-by: Nok Lam Chan <[email protected]> Signed-off-by: Nok <[email protected]>
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
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,71 @@ | ||
"""This module is to test the behaviour change based on defined config in pandera""" | ||
# pylint:disable=import-outside-toplevel,abstract-method,redefined-outer-name | ||
|
||
|
||
import pandas as pd | ||
import pytest | ||
|
||
import pandera as pa | ||
from pandera import DataFrameModel, DataFrameSchema, SeriesSchema | ||
from pandera.config import CONFIG, ValidationDepth | ||
|
||
|
||
@pytest.fixture() | ||
def disable_validation(): | ||
"""Fixture to disable validation and clean up after the test is finished""" | ||
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"] | ||
) | ||
# pylint: disable=unused-argument | ||
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]) | ||
# pylint: disable=unused-argument | ||
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 |