pandera.schema_components.MultiIndex

class pandera.schema_components.MultiIndex(indexes, coerce=False, strict=False, name=None, ordered=True)[source]

Validate types and properties of a DataFrame MultiIndex.

This class inherits from DataFrameSchema to leverage its validation logic.

Create MultiIndex validator.

Parameters
  • indexes (List[Index]) – list of Index validators for each level of the MultiIndex index.

  • coerce (bool) – Whether or not to coerce the MultiIndex to the specified pandas_dtypes before validation

  • strict (bool) – whether or not to accept columns in the MultiIndex that aren’t defined in the indexes argument.

  • name (Optional[str]) – name of schema component

  • ordered (bool) – whether or not to validate the indexes order.

Example

>>> import pandas as pd
>>> import pandera as pa
>>>
>>>
>>> schema = pa.DataFrameSchema(
...     columns={"column": pa.Column(pa.Int)},
...     index=pa.MultiIndex([
...         pa.Index(pa.String,
...               pa.Check(lambda s: s.isin(["foo", "bar"])),
...               name="index0"),
...         pa.Index(pa.Int, name="index1"),
...     ])
... )
>>>
>>> df = pd.DataFrame(
...     data={"column": [1, 2, 3]},
...     index=pd.MultiIndex.from_arrays(
...         [["foo", "bar", "foo"], [0, 1, 2]],
...         names=["index0", "index1"],
...     )
... )
>>>
>>> schema.validate(df)
               column
index0 index1
foo    0            1
bar    1            2
foo    2            3

See here for more usage details.

Attributes

coerce

Whether or not to coerce data types.

dtype

A pandas style dtype dict where the keys are column names and values are pandas dtype for the column.

names

Get index names in the MultiIndex schema component.

ordered

Whether or not to validate the columns order.

pandas_dtype

Get the pandas dtype property.

pdtype

PandasDtype of the dataframe.

Methods

__init__

Create MultiIndex validator.

coerce_dtype

Coerce type of a pd.Series by type specified in pandas_dtype.

example

Generate an example of a particular size.

strategy

Create a hypothesis strategy for generating a DataFrame.

validate

Validate DataFrame or Series MultiIndex.

__call__

Alias for DataFrameSchema.validate() method.