Skip to content
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
2 changes: 1 addition & 1 deletion pandera/io/pandas_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def to_script(dataframe_schema, path_or_buf=None, *, minimal: bool = True):

script = SCRIPT_TEMPLATE.format(
columns=column_str,
checks=statistics["checks"],
checks=_format_checks(statistics["checks"]),
index=index,
dtype=dataframe_schema.dtype,
coerce=dataframe_schema.coerce,
Expand Down
22 changes: 22 additions & 0 deletions tests/io/test_pandas_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,28 @@ def test_to_script(index):
assert schema == schema_to_write


@pytest.mark.skipif(
platform.system() == "Windows",
reason="skipping due to issues with opening file names for temp files.",
)
def test_to_script_dataframe_level_checks():
"""Test that dataframe-level checks survive a to_script round trip."""
schema_to_write = pandera.DataFrameSchema(
{"a": pandera.Column(int)},
checks=[pandera.Check.gt(0)],
)

local_dict = {}
# pylint: disable=exec-used
exec(schema_to_write.to_script(minimal=False), globals(), local_dict)
schema = local_dict["schema"]

assert schema == schema_to_write
schema.validate(pd.DataFrame({"a": [1, 2]}))
with pytest.raises(pandera.errors.SchemaError):
schema.validate(pd.DataFrame({"a": [-1]}))


def test_to_script_lambda_check():
"""Test writing DataFrameSchema to a script with lambda check."""
schema1 = pandera.DataFrameSchema(
Expand Down
Loading