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

Add details in errors reporting to ease data debugging. #193

Merged
merged 5 commits into from
Jun 20, 2020
Merged
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
23 changes: 18 additions & 5 deletions pandera/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ def __init__(

self.columns = {} if columns is None else columns

if coerce and None in [c.pandas_dtype for c in self.columns.values()]:
raise errors.SchemaInitError(
"Must specify dtype in all Columns if coercing "
"DataFrameSchema")
if coerce:
missing_pandas_type = [
name for name, col in self.columns.items()
if col.pandas_dtype is None
]
if missing_pandas_type:
raise errors.SchemaInitError(
"Must specify dtype in all Columns if coercing "
"DataFrameSchema ; columns with missing pandas_type:" +
", ".join(missing_pandas_type))

self.checks = checks
self.index = index
Expand Down Expand Up @@ -507,7 +513,14 @@ def coerce_dtype(
# only coerce non-null elements to string
return series_or_index.where(
series_or_index.isna(), series_or_index.astype(str))
return series_or_index.astype(self.dtype)

try:
return series_or_index.astype(self.dtype)
except TypeError as exc:
msg = "Error while coercing '%s' to type %s" % (
self.name, self.dtype
)
raise TypeError(msg) from exc

@property
def _allow_groupby(self):
Expand Down