Skip to content

Commit

Permalink
add test and PANDERA_VALIDATION_ENABLED support for both pandas serie…
Browse files Browse the repository at this point in the history
…s and dataframe

Signed-off-by: Nok <[email protected]>
  • Loading branch information
noklam committed Nov 3, 2023
1 parent 4ab1b7d commit 9854ab9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/source/configuration.rst
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`.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ page or reach out to the maintainers and pandera community on
data_format_conversion
supported_libraries
integrations
configuration

.. toctree::
:maxdepth: 6
Expand Down
5 changes: 4 additions & 1 deletion pandera/api/pandas/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import copy
import warnings
from typing import Any, List, Optional, TypeVar, Union, cast

from pandera.config import CONFIG
import pandas as pd

from pandera import errors
Expand Down Expand Up @@ -426,6 +426,9 @@ def validate( # type: ignore [override]
dtype: float64
"""
if not CONFIG.validation_enabled:
return check_obj

if self._is_inferred:
warnings.warn(
f"This {type(self)} is an inferred schema that hasn't been "
Expand Down
2 changes: 1 addition & 1 deletion pandera/api/pandas/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def validate(
5 0.76 dog
"""
if not CONFIG.validation_enabled:
return
return check_obj

# NOTE: Move this into its own schema-backend variant. This is where
# the benefits of separating the schema spec from the backend
Expand Down
69 changes: 69 additions & 0 deletions tests/core/test_pandas_config.py
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

0 comments on commit 9854ab9

Please sign in to comment.