pandera.api.pandas.array.SeriesSchema¶
- class pandera.api.pandas.array.SeriesSchema(dtype=None, checks=None, parsers=None, index=None, nullable=False, unique=False, report_duplicates='all', coerce=False, name=None, title=None, description=None, default=None, metadata=None, drop_invalid_rows=False)[source]¶
A pandas Series validator.
Initialize series schema base object.
- Parameters:
dtype (
Union
[str
,type
,DataType
,Type
,ExtensionDtype
,dtype
,None
]) – datatype of the column. If a string is specified, then assumes one of the valid pandas string values: http://pandas.pydata.org/pandas-docs/stable/basics.html#dtypeschecks (
Union
[Check
,List
[Union
[Check
,Hypothesis
]],None
]) –If element_wise is True, then callable signature should be:
Callable[Any, bool]
where theAny
input is a scalar element in the column. Otherwise, the input is assumed to be a pandas.Series object.index – specify the datatypes and properties of the index.
nullable (
bool
) – Whether or not column can contain null values.unique (
bool
) – Whether or not column can contain duplicate values.report_duplicates (
Union
[Literal
[‘exclude_first’],Literal
[‘exclude_last’],Literal
[‘all’]]) – how to report unique errors - exclude_first: report all duplicates except first occurence - exclude_last: report all duplicates except last occurence - all: (default) report all duplicatescoerce (
bool
) – If True, when schema.validate is called the column will be coerced into the specified dtype. This has no effect on columns wheredtype=None
.title (
Optional
[str
]) – A human-readable label for the series.description (
Optional
[str
]) – An arbitrary textual description of the series.default (
Optional
[Any
]) – The default value for missing values in the series.drop_invalid_rows (
bool
) – if True, drop invalid rows on validation.
Attributes
BACKEND_REGISTRY
dtype
Get the pandas dtype
properties
Get the properties of the schema for serialization purposes.
Methods
- __init__(dtype=None, checks=None, parsers=None, index=None, nullable=False, unique=False, report_duplicates='all', coerce=False, name=None, title=None, description=None, default=None, metadata=None, drop_invalid_rows=False)[source]¶
Initialize series schema base object.
- Parameters:
dtype (
Union
[str
,type
,DataType
,Type
,ExtensionDtype
,dtype
,None
]) – datatype of the column. If a string is specified, then assumes one of the valid pandas string values: http://pandas.pydata.org/pandas-docs/stable/basics.html#dtypeschecks (
Union
[Check
,List
[Union
[Check
,Hypothesis
]],None
]) –If element_wise is True, then callable signature should be:
Callable[Any, bool]
where theAny
input is a scalar element in the column. Otherwise, the input is assumed to be a pandas.Series object.index – specify the datatypes and properties of the index.
nullable (
bool
) – Whether or not column can contain null values.unique (
bool
) – Whether or not column can contain duplicate values.report_duplicates (
Union
[Literal
[‘exclude_first’],Literal
[‘exclude_last’],Literal
[‘all’]]) – how to report unique errors - exclude_first: report all duplicates except first occurence - exclude_last: report all duplicates except last occurence - all: (default) report all duplicatescoerce (
bool
) – If True, when schema.validate is called the column will be coerced into the specified dtype. This has no effect on columns wheredtype=None
.title (
Optional
[str
]) – A human-readable label for the series.description (
Optional
[str
]) – An arbitrary textual description of the series.default (
Optional
[Any
]) – The default value for missing values in the series.drop_invalid_rows (
bool
) – if True, drop invalid rows on validation.
- example(size=None)[source]¶
Generate an example of a particular size.
- Parameters:
size – number of elements in the generated Series.
- Return type:
- Returns:
pandas Series object.
- validate(check_obj, head=None, tail=None, sample=None, random_state=None, lazy=False, inplace=False)[source]¶
Validate a Series object.
- Parameters:
check_obj (
Series
) – One-dimensional ndarray with axis labels (including time series).head (
Optional
[int
]) – validate the first n rows. Rows overlapping with tail or sample are de-duplicated.tail (
Optional
[int
]) – validate the last n rows. Rows overlapping with head or sample are de-duplicated.sample (
Optional
[int
]) – validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.random_state (
Optional
[int
]) – random seed for thesample
argument.lazy (
bool
) – if True, lazily evaluates dataframe against all validation checks and raises aSchemaErrors
. Otherwise, raiseSchemaError
as soon as one occurs.inplace (
bool
) – if True, applies coercion to the object of validation, otherwise creates a copy of the data.
- Return type:
- Returns:
validated Series.
- Raises:
SchemaError – when
DataFrame
violates built-in or custom checks.- Example:
>>> import pandas as pd >>> import pandera as pa >>> >>> series_schema = pa.SeriesSchema( ... float, [ ... pa.Check(lambda s: s > 0), ... pa.Check(lambda s: s < 1000), ... pa.Check(lambda s: s.mean() > 300), ... ]) >>> series = pd.Series([1, 100, 800, 900, 999], dtype=float) >>> print(series_schema.validate(series)) 0 1.0 1 100.0 2 800.0 3 900.0 4 999.0 dtype: float64