validating columns in pandas on the basis of dtypes

Solutions on MaxInterview for validating columns in pandas on the basis of dtypes by the best coders in the world

showing results for - "validating columns in pandas on the basis of dtypes"
Luis
11 Aug 2020
1import pandas as pd
2import pandera as pa
3
4df = pd.DataFrame({
5    "column1": [1, 2, 3],
6    "column2": ["a", "b", "c"],
7})
8
9column1_schema = pa.Column(pa.Int, name="column1")
10column2_schema = pa.Column(pa.String, name="column2")
11
12# pass the dataframe as an argument to the Column object callable
13df = column1_schema(df)
14validated_df = column2_schema(df)
15
16# or explicitly use the validate method
17df = column1_schema.validate(df)
18validated_df = column2_schema.validate(df)
19
20# use the DataFrame.pipe method to validate two columns
21validated_df = df.pipe(column1_schema).pipe(column2_schema)
22