From 6d08db18094964c36dab27cdba0213a91964cadb Mon Sep 17 00:00:00 2001 From: Nemanja Radojkovic Date: Wed, 17 Jun 2020 19:25:16 +0200 Subject: [PATCH 1/4] from_yaml bug fix (index:null) --- pandera/io.py | 4 ++- tests/test_io.py | 67 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/pandera/io.py b/pandera/io.py index d26484f9b..43f64cf98 100644 --- a/pandera/io.py +++ b/pandera/io.py @@ -156,7 +156,9 @@ def _deserialize_schema(serialized_schema): for index_component in serialized_schema["index"] ] - if len(index) == 1: + if isinstance(index, type(None)): + index = None + elif len(index) == 1: index = Index(**index[0]) else: index = MultiIndex(indexes=[ diff --git a/tests/test_io.py b/tests/test_io.py index 65c4ac2b5..59148fc05 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -125,6 +125,63 @@ def _create_schema(index="single"): """.format(version=pa.__version__) +def _create_schema_null_index(): + + return pa.DataFrameSchema( + columns={ + "float_column": pa.Column( + pa.Float, checks=[ + pa.Check.greater_than(-10), + pa.Check.less_than(20), + pa.Check.in_range(-10, 20), + ] + ), + "str_column": pa.Column( + pa.String, checks=[ + pa.Check.isin(["foo", "bar", "x", "xy"]), + pa.Check.str_length(1, 3) + ] + ), + }, + index=None + ) + + +YAML_SCHEMA_NULL_INDEX = """ +schema_type: dataframe +version: {version} +columns: + float_column: + pandas_dtype: float + nullable: false + checks: + greater_than: -10 + less_than: 20 + in_range: + min_value: -10 + max_value: 20 + str_column: + pandas_dtype: string + nullable: false + checks: + isin: + - foo + - bar + - x + - xy + str_length: + min_value: 1 + max_value: 3 +index: null +coerce: false +strict: false +""".format(version=pa.__version__) + +YAML_VALIDATION_PAIRS = [ + [YAML_SCHEMA, _create_schema], + [YAML_SCHEMA_NULL_INDEX, _create_schema_null_index] +] + @pytest.mark.skipif( PYYAML_VERSION.release < (5, 1, 0), # type: ignore reason="pyyaml >= 5.1.0 required", @@ -162,10 +219,12 @@ def test_to_yaml(): ) def test_from_yaml(): """Test that from_yaml reads yaml string.""" - schema_from_yaml = io.from_yaml(YAML_SCHEMA) - expected_schema = _create_schema() - assert schema_from_yaml == expected_schema - assert expected_schema == schema_from_yaml + + for yml_string, schema_creator in YAML_VALIDATION_PAIRS: + schema_from_yaml = io.from_yaml(yml_string) + expected_schema = schema_creator() + assert schema_from_yaml == expected_schema + assert expected_schema == schema_from_yaml def test_io_yaml_file_obj(): From f4f331264b07b2b3218e09d839b1d3d0fe557ed6 Mon Sep 17 00:00:00 2001 From: Nemanja Radojkovic Date: Wed, 17 Jun 2020 20:06:21 +0200 Subject: [PATCH 2/4] whitespace --- tests/test_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_io.py b/tests/test_io.py index 59148fc05..4d3366ed4 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -145,7 +145,7 @@ def _create_schema_null_index(): }, index=None ) - + YAML_SCHEMA_NULL_INDEX = """ schema_type: dataframe From 0d93c98088b42a45e2e8bed01e1b84384be1c851 Mon Sep 17 00:00:00 2001 From: Niels Bantilan Date: Wed, 17 Jun 2020 14:46:32 -0400 Subject: [PATCH 3/4] fix trailing whitespace --- tests/test_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_io.py b/tests/test_io.py index 4d3366ed4..611308a5d 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -219,7 +219,7 @@ def test_to_yaml(): ) def test_from_yaml(): """Test that from_yaml reads yaml string.""" - + for yml_string, schema_creator in YAML_VALIDATION_PAIRS: schema_from_yaml = io.from_yaml(yml_string) expected_schema = schema_creator() From b1ca7e7392eccb481f9b6ac234d65f69ff244cc0 Mon Sep 17 00:00:00 2001 From: Niels Bantilan Date: Wed, 17 Jun 2020 16:08:31 -0400 Subject: [PATCH 4/4] Update io.py --- pandera/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandera/io.py b/pandera/io.py index 43f64cf98..5d9f8fa13 100644 --- a/pandera/io.py +++ b/pandera/io.py @@ -156,7 +156,7 @@ def _deserialize_schema(serialized_schema): for index_component in serialized_schema["index"] ] - if isinstance(index, type(None)): + if index is None: index = None elif len(index) == 1: index = Index(**index[0])