|
1 | 1 | """Core pandera schema class definitions."""
|
2 | 2 |
|
| 3 | +import json |
| 4 | + |
3 | 5 | import pandas as pd
|
4 | 6 |
|
5 | 7 | from typing import Optional
|
|
8 | 10 | from .checks import Check
|
9 | 11 |
|
10 | 12 |
|
| 13 | +N_INDENT_SPACES = 4 |
| 14 | + |
| 15 | + |
11 | 16 | class DataFrameSchema(object):
|
12 | 17 | """A light-weight pandas DataFrame validator."""
|
13 | 18 |
|
@@ -52,6 +57,7 @@ def __init__(
|
52 | 57 | self.coerce = coerce
|
53 | 58 | self.strict = strict
|
54 | 59 | self._validate_schema()
|
| 60 | + self._set_column_names() |
55 | 61 |
|
56 | 62 | def __call__(
|
57 | 63 | self,
|
@@ -87,6 +93,12 @@ def _validate_schema(self):
|
87 | 93 | "specified in the DataFrameSchema." %
|
88 | 94 | (nonexistent_groupby_columns, column_name))
|
89 | 95 |
|
| 96 | + def _set_column_names(self): |
| 97 | + self.columns = { |
| 98 | + column_name: column.set_name(column_name) |
| 99 | + for column_name, column in self.columns.items() |
| 100 | + } |
| 101 | + |
90 | 102 | @staticmethod
|
91 | 103 | def _dataframe_to_validate(
|
92 | 104 | dataframe: pd.DataFrame,
|
@@ -168,6 +180,40 @@ def validate(
|
168 | 180 | dataframe = self.transformer(dataframe)
|
169 | 181 | return dataframe
|
170 | 182 |
|
| 183 | + def __repr__(self): |
| 184 | + return "%s(columns=%s, index=%s, transformer=%s, coerce=%s)" % \ |
| 185 | + (self.__class__.__name__, |
| 186 | + self.columns, |
| 187 | + self.index, |
| 188 | + self.transformer, |
| 189 | + self.coerce) |
| 190 | + |
| 191 | + def __str__(self): |
| 192 | + columns = {k: str(v) for k, v in self.columns.items()} |
| 193 | + columns = json.dumps(columns, indent=N_INDENT_SPACES) |
| 194 | + _indent = " " * N_INDENT_SPACES |
| 195 | + columns = "\n".join( |
| 196 | + "{}{}".format(_indent, line) if i != 0 |
| 197 | + else "{}columns={}".format(_indent, line) |
| 198 | + for i, line in enumerate(columns.split("\n"))) |
| 199 | + return ( |
| 200 | + "{class_name}(\n" |
| 201 | + "{columns},\n" |
| 202 | + "{indent}index={index},\n" |
| 203 | + "{indent}transformer={transformer},\n" |
| 204 | + "{indent}coerce={coerce},\n" |
| 205 | + "{indent}strict={strict}\n" |
| 206 | + ")" |
| 207 | + ).format( |
| 208 | + class_name=self.__class__.__name__, |
| 209 | + columns=columns, |
| 210 | + index=str(self.index), |
| 211 | + transformer=str(self.transformer), |
| 212 | + coerce=self.coerce, |
| 213 | + strict=self.strict, |
| 214 | + indent=_indent, |
| 215 | + ) |
| 216 | + |
171 | 217 |
|
172 | 218 | class SeriesSchemaBase(object):
|
173 | 219 | """Base series validator object."""
|
|
0 commit comments