bikes.core.schemas

Define and validate dataframe schemas.

  1"""Define and validate dataframe schemas."""
  2
  3# %% IMPORTS
  4
  5import typing as T
  6
  7import pandas as pd
  8import pandera.pandas as pa
  9import pandera.typing.pandas as papd
 10
 11# %% TYPES
 12
 13# Generic type for a dataframe container
 14TSchema = T.TypeVar("TSchema", bound="pa.DataFrameModel")
 15
 16# %% SCHEMAS
 17
 18
 19class Schema(pa.DataFrameModel):
 20    """Base class for a dataframe schema.
 21
 22    Use a schema to type your dataframe object.
 23    e.g., to communicate and validate its fields.
 24    """
 25
 26    class Config:
 27        """Default configurations for all schemas.
 28
 29        Parameters:
 30            coerce (bool): convert data type if possible.
 31            strict (bool): ensure the data type is correct.
 32        """
 33
 34        coerce: bool = True
 35        strict: bool = True
 36
 37    @classmethod
 38    def check(cls: type[TSchema], data: pd.DataFrame) -> papd.DataFrame[TSchema]:
 39        """Check the dataframe with this schema.
 40
 41        Args:
 42            data (pd.DataFrame): dataframe to check.
 43
 44        Returns:
 45            papd.DataFrame[TSchema]: validated dataframe.
 46        """
 47        return cls.validate(data)
 48
 49
 50class InputsSchema(Schema):
 51    """Schema for the project inputs."""
 52
 53    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
 54    dteday: papd.Series[papd.DateTime] = pa.Field()
 55    season: papd.Series[papd.UInt8] = pa.Field(isin=[1, 2, 3, 4])
 56    yr: papd.Series[papd.UInt8] = pa.Field(ge=0, le=1)
 57    mnth: papd.Series[papd.UInt8] = pa.Field(ge=1, le=12)
 58    hr: papd.Series[papd.UInt8] = pa.Field(ge=0, le=23)
 59    holiday: papd.Series[papd.Bool] = pa.Field()
 60    weekday: papd.Series[papd.UInt8] = pa.Field(ge=0, le=6)
 61    workingday: papd.Series[papd.Bool] = pa.Field()
 62    weathersit: papd.Series[papd.UInt8] = pa.Field(ge=1, le=4)
 63    temp: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
 64    atemp: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
 65    hum: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
 66    windspeed: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
 67    casual: papd.Series[papd.UInt32] = pa.Field(ge=0)
 68    registered: papd.Series[papd.UInt32] = pa.Field(ge=0)
 69
 70
 71Inputs = papd.DataFrame[InputsSchema]
 72
 73
 74class TargetsSchema(Schema):
 75    """Schema for the project target."""
 76
 77    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
 78    cnt: papd.Series[papd.UInt32] = pa.Field(ge=0)
 79
 80
 81Targets = papd.DataFrame[TargetsSchema]
 82
 83
 84class OutputsSchema(Schema):
 85    """Schema for the project output."""
 86
 87    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
 88    prediction: papd.Series[papd.UInt32] = pa.Field(ge=0)
 89
 90
 91Outputs = papd.DataFrame[OutputsSchema]
 92
 93
 94class SHAPValuesSchema(Schema):
 95    """Schema for the project shap values."""
 96
 97    class Config:
 98        """Default configurations this schema.
 99
100        Parameters:
101            dtype (str): dataframe default data type.
102            strict (bool): ensure the data type is correct.
103        """
104
105        dtype: str = "float32"
106        strict: bool = False
107
108
109SHAPValues = papd.DataFrame[SHAPValuesSchema]
110
111
112class FeatureImportancesSchema(Schema):
113    """Schema for the project feature importances."""
114
115    feature: papd.Series[papd.String] = pa.Field()
116    importance: papd.Series[papd.Float32] = pa.Field()
117
118
119FeatureImportances = papd.DataFrame[FeatureImportancesSchema]
class Schema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
20class Schema(pa.DataFrameModel):
21    """Base class for a dataframe schema.
22
23    Use a schema to type your dataframe object.
24    e.g., to communicate and validate its fields.
25    """
26
27    class Config:
28        """Default configurations for all schemas.
29
30        Parameters:
31            coerce (bool): convert data type if possible.
32            strict (bool): ensure the data type is correct.
33        """
34
35        coerce: bool = True
36        strict: bool = True
37
38    @classmethod
39    def check(cls: type[TSchema], data: pd.DataFrame) -> papd.DataFrame[TSchema]:
40        """Check the dataframe with this schema.
41
42        Args:
43            data (pd.DataFrame): dataframe to check.
44
45        Returns:
46            papd.DataFrame[TSchema]: validated dataframe.
47        """
48        return cls.validate(data)

Base class for a dataframe schema.

Use a schema to type your dataframe object. e.g., to communicate and validate its fields.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
Schema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
@classmethod
def check( cls: type[~TSchema], data: pandas.core.frame.DataFrame) -> pandera.typing.pandas.DataFrame[~TSchema]:
38    @classmethod
39    def check(cls: type[TSchema], data: pd.DataFrame) -> papd.DataFrame[TSchema]:
40        """Check the dataframe with this schema.
41
42        Args:
43            data (pd.DataFrame): dataframe to check.
44
45        Returns:
46            papd.DataFrame[TSchema]: validated dataframe.
47        """
48        return cls.validate(data)

Check the dataframe with this schema.

Arguments:
  • data (pd.DataFrame): dataframe to check.
Returns:

papd.DataFrame[TSchema]: validated dataframe.

class InputsSchema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
51class InputsSchema(Schema):
52    """Schema for the project inputs."""
53
54    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
55    dteday: papd.Series[papd.DateTime] = pa.Field()
56    season: papd.Series[papd.UInt8] = pa.Field(isin=[1, 2, 3, 4])
57    yr: papd.Series[papd.UInt8] = pa.Field(ge=0, le=1)
58    mnth: papd.Series[papd.UInt8] = pa.Field(ge=1, le=12)
59    hr: papd.Series[papd.UInt8] = pa.Field(ge=0, le=23)
60    holiday: papd.Series[papd.Bool] = pa.Field()
61    weekday: papd.Series[papd.UInt8] = pa.Field(ge=0, le=6)
62    workingday: papd.Series[papd.Bool] = pa.Field()
63    weathersit: papd.Series[papd.UInt8] = pa.Field(ge=1, le=4)
64    temp: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
65    atemp: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
66    hum: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
67    windspeed: papd.Series[papd.Float16] = pa.Field(ge=0, le=1)
68    casual: papd.Series[papd.UInt32] = pa.Field(ge=0)
69    registered: papd.Series[papd.UInt32] = pa.Field(ge=0)

Schema for the project inputs.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
InputsSchema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
instant: pandera.typing.pandas.Index[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

dteday: pandera.typing.pandas.Series[pandera.dtypes.Timestamp]

Captures extra information about a field.

new in 0.5.0

season: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

yr: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

mnth: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

hr: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

holiday: pandera.typing.pandas.Series[pandera.dtypes.Bool]

Captures extra information about a field.

new in 0.5.0

weekday: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

workingday: pandera.typing.pandas.Series[pandera.dtypes.Bool]

Captures extra information about a field.

new in 0.5.0

weathersit: pandera.typing.pandas.Series[pandera.dtypes.UInt8]

Captures extra information about a field.

new in 0.5.0

temp: pandera.typing.pandas.Series[pandera.dtypes.Float16]

Captures extra information about a field.

new in 0.5.0

atemp: pandera.typing.pandas.Series[pandera.dtypes.Float16]

Captures extra information about a field.

new in 0.5.0

hum: pandera.typing.pandas.Series[pandera.dtypes.Float16]

Captures extra information about a field.

new in 0.5.0

windspeed: pandera.typing.pandas.Series[pandera.dtypes.Float16]

Captures extra information about a field.

new in 0.5.0

casual: pandera.typing.pandas.Series[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

registered: pandera.typing.pandas.Series[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

Inherited Members
Schema
check
Inputs = pandera.typing.pandas.DataFrame[InputsSchema]
class TargetsSchema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
75class TargetsSchema(Schema):
76    """Schema for the project target."""
77
78    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
79    cnt: papd.Series[papd.UInt32] = pa.Field(ge=0)

Schema for the project target.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
TargetsSchema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
instant: pandera.typing.pandas.Index[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

cnt: pandera.typing.pandas.Series[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

Inherited Members
Schema
check
Targets = pandera.typing.pandas.DataFrame[TargetsSchema]
class OutputsSchema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
85class OutputsSchema(Schema):
86    """Schema for the project output."""
87
88    instant: papd.Index[papd.UInt32] = pa.Field(ge=0)
89    prediction: papd.Series[papd.UInt32] = pa.Field(ge=0)

Schema for the project output.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
OutputsSchema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
instant: pandera.typing.pandas.Index[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

prediction: pandera.typing.pandas.Series[pandera.dtypes.UInt32]

Captures extra information about a field.

new in 0.5.0

Inherited Members
Schema
check
Outputs = pandera.typing.pandas.DataFrame[OutputsSchema]
class SHAPValuesSchema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
 95class SHAPValuesSchema(Schema):
 96    """Schema for the project shap values."""
 97
 98    class Config:
 99        """Default configurations this schema.
100
101        Parameters:
102            dtype (str): dataframe default data type.
103            strict (bool): ensure the data type is correct.
104        """
105
106        dtype: str = "float32"
107        strict: bool = False

Schema for the project shap values.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
SHAPValuesSchema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
Inherited Members
Schema
check
SHAPValues = pandera.typing.pandas.DataFrame[SHAPValuesSchema]
class FeatureImportancesSchema(typing.Generic[~TDataFrame, ~TSchema], pandera.api.base.model.BaseModel):
113class FeatureImportancesSchema(Schema):
114    """Schema for the project feature importances."""
115
116    feature: papd.Series[papd.String] = pa.Field()
117    importance: papd.Series[papd.Float32] = pa.Field()

Schema for the project feature importances.

@docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
FeatureImportancesSchema(*args, **kwargs)
275    @docstring_substitution(validate_doc=BaseSchema.validate.__doc__)
276    def __new__(cls, *args, **kwargs) -> DataFrameBase[Self]:  # type: ignore [misc]
277        """%(validate_doc)s"""
278        return cast(DataFrameBase[Self], cls.validate(*args, **kwargs))

Validate a DataFrame based on the schema specification.

Parameters
  • pd.DataFrame check_obj: the dataframe to be validated.
  • head: validate the first n rows. Rows overlapping with tail or sample are de-duplicated.
  • tail: validate the last n rows. Rows overlapping with head or sample are de-duplicated.
  • sample: validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.
  • random_state: random seed for the sample argument.
  • lazy: if True, lazily evaluates dataframe against all validation checks and raises a SchemaErrors. Otherwise, raise SchemaError as soon as one occurs.
  • inplace: if True, applies coercion to the object of validation, otherwise creates a copy of the data. :returns: validated DataFrame
Raises
  • SchemaError: when DataFrame violates built-in or custom checks.
feature: pandera.typing.pandas.Series[pandera.dtypes.String]

Captures extra information about a field.

new in 0.5.0

importance: pandera.typing.pandas.Series[pandera.dtypes.Float32]

Captures extra information about a field.

new in 0.5.0

Inherited Members
Schema
check
FeatureImportances = pandera.typing.pandas.DataFrame[FeatureImportancesSchema]