Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty data type not supported for serialization #435

Merged
merged 2 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 18 additions & 5 deletions pandera/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ def _serialize_component_stats(component_stats):
serialized_checks[check_name] = _serialize_check_stats(
check_stats, component_stats["pandas_dtype"]
)

pandas_dtype = component_stats.get("pandas_dtype", None)
if pandas_dtype:
pandas_dtype = pandas_dtype.value

return {
"pandas_dtype": component_stats["pandas_dtype"].value,
"pandas_dtype": pandas_dtype,
"nullable": component_stats["nullable"],
"checks": serialized_checks,
**{
Expand Down Expand Up @@ -134,9 +139,10 @@ def handle_stat_dtype(stat):
def _deserialize_component_stats(serialized_component_stats):
from pandera import Check # pylint: disable=import-outside-toplevel

pandas_dtype = PandasDtype.from_str_alias(
serialized_component_stats["pandas_dtype"]
)
pandas_dtype = serialized_component_stats.get("pandas_dtype", None)
if pandas_dtype:
pandas_dtype = PandasDtype.from_str_alias(pandas_dtype)

checks = None
if serialized_component_stats.get("checks") is not None:
checks = [
Expand Down Expand Up @@ -333,8 +339,15 @@ def to_script(dataframe_schema, path_or_buf=None):

columns = {}
for colname, properties in statistics["columns"].items():
try:
jeffzi marked this conversation as resolved.
Show resolved Hide resolved
pandas_dtype = (
f"PandasDtype.{properties.get('pandas_dtype', None).name}"
)
except AttributeError:
pandas_dtype = None

column_code = COLUMN_TEMPLATE.format(
pandas_dtype=f"PandasDtype.{properties['pandas_dtype'].name}",
pandas_dtype=pandas_dtype,
checks=_format_checks(properties["checks"]),
nullable=properties["nullable"],
allow_duplicates=properties["allow_duplicates"],
Expand Down
9 changes: 9 additions & 0 deletions tests/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def _create_schema(index="single"):
regex=True,
checks=[pa.Check.str_length(1, 3)],
),
"empty_column": pa.Column(),
},
index=index,
coerce=False,
Expand Down Expand Up @@ -183,6 +184,14 @@ def _create_schema(index="single"):
coerce: true
required: false
regex: true
empty_column:
pandas_dtype: null
nullable: false
checks: null
allow_duplicates: true
coerce: false
required: true
regex: false
index:
- pandas_dtype: int
nullable: false
Expand Down