pandera.api.pyspark.model.DataFrameModelΒΆ
- class pandera.api.pyspark.model.DataFrameModel(*args, **kwargs)[source]ΒΆ
Definition of a
DataFrameSchema.new in 0.16.0
See the User Guide for more.
Check if all columns in a dataframe have a column in the Schema.
- Parameters:
check_obj β DataFrame object i.e. the dataframe to be validated.
head β Not used since spark has no concept of head or tail
tail β Not used since spark has no concept of head or tail
sample β validate a random sample of n% rows. Value ranges from 0-1, for example 10% rows can be sampled using setting value as 0.1. refer below documentation. https://spark.apache.org/docs/3.1.2/api/python/reference/api/pyspark.sql.DataFrame.sample.html
random_state β random seed for the
sampleargument.lazy β if True, lazily evaluates dataframe against all validation checks and raises a
SchemaErrors. Otherwise, raiseSchemaErroras 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
DataFrameviolates built-in or custom checks.- Example:
Calling
schema.validatereturns the dataframe.>>> import pandera.pyspark as psa >>> from pyspark.sql import SparkSession >>> import pyspark.sql.types as T >>> spark = SparkSession.builder.getOrCreate() >>> data = [("Bread", 9), ("Butter", 15)] >>> spark_schema = T.StructType( ... [ ... T.StructField("product", T.StringType(), False), ... T.StructField("price", T.IntegerType(), False), ... ], ... ) >>> df = spark.createDataFrame(data=data, schema=spark_schema) >>> schema_with_checks = psa.DataFrameSchema( ... columns={ ... "product": psa.Column("str", checks=psa.Check.str_startswith("B")), ... "price": psa.Column("int", checks=psa.Check.gt(5)), ... }, ... name="product_schema", ... description="schema for product info", ... title="ProductSchema", ... ) >>> schema_with_checks.validate(df).take(2)
Methods
- classmethod to_ddl()[source]ΒΆ
Recover fields of DataFrameModel as a PySpark DDL string.
- Return type:
- Returns:
String with current model fields, in compact DDL format.
- classmethod to_structtype()[source]ΒΆ
Recover fields of DataFrameModel as a PySpark StructType object.
- Return type:
- Returns:
StructType object with current model fields.
- classmethod validate(check_obj, head=None, tail=None, sample=None, random_state=None, lazy=True, inplace=False)[source]ΒΆ
Validate a DataFrame based on the schema specification.
- Parameters:
check_obj (pd.DataFrame) β the dataframe to be validated.
head (
UnionType[int,None]) β validate the first n rows. Rows overlapping with tail or sample are de-duplicated.tail (
UnionType[int,None]) β validate the last n rows. Rows overlapping with head or sample are de-duplicated.sample (
UnionType[int,None]) β validate a random sample of n rows. Rows overlapping with head or tail are de-duplicated.random_state (
UnionType[int,None]) β random seed for thesampleargument.lazy (
bool) β if True, lazily evaluates dataframe against all validation checks and raises aSchemaErrors. Otherwise, raiseSchemaErroras soon as one occurs.inplace (
bool) β if True, applies coercion to the object of validation, otherwise creates a copy of the data.
- Return type:
DataFrameBase[Self]- Returns:
validated
DataFrame- Raises:
SchemaError β when
DataFrameviolates built-in or custom checks.