Package: valido
Valido ðŸŒ©PySpark âš¡ dataframe workflow âš’ validatorDescriptionIn projects using PySpark, it's very common to have functions that take Spark DataFrames as input or produce them as
output. It's hard to figure out quickly what these DataFrames contain. This library offers simple decorators to annotate
your functions so that they document themselves and that documentation is kept up-to-date by validating the input and
output on runtime.For example,@df_in(columns=["Brand","Price"])# the function expects a DataFrame as input parameter with columns Brand and Price@df_out(columns=["Brand","Price"])# the function will return a DataFrame with columns Brand and Pricedeffilter_cars(car_df):# before this code is executed, the input DataFrame is validated according to the above decorator# filter some cars..returnfiltered_cars_dfTable of ContentsInstallationUsageContributingTestsLicenseInstallationInstall with your favorite Python dependency manager (pip) likepipinstallvalidoUsageStart by importing the needed decorators:fromvalidoimportdf_in,df_outTo check a DataFrame input to a function, annotate the function with@df_in. For example the following function
expects to get a DataFrame with columnsBrandandPrice:@df_in(columns=["Brand","Price"])defprocess_cars(car_df):# do stuff with carsIf your function takes multiple arguments, specify the field to be checked with it'sname:@df_in(name="car_df",columns=["Brand","Price"])defprocess_cars(year,style,car_df):# do stuff with carsNote:Since this will evaluate it at runtime please use named arguments in the function call like this:process_cars(year = 2021, style = "Mazda", car_df = mydf)To check that a function returns a DataFrame with specific columns, use@df_outdecorator:@df_out(columns=["Brand","Price"])defget_all_cars():# get those carsreturnall_cars_dfIn case one of the listed columns is missing from the DataFrame, a helpful assertion error is thrown:AssertionError("Column Price missing from DataFrame. Got columns: ['Brand']")To check both input and output, just use both annotations on the same function:@df_in(columns=["Brand","Price"])@df_out(columns=["Brand","Price"])deffilter_cars(car_df):# filter some carsreturnfiltered_cars_dfIf you want to also check the data types of each column, you can replace the column array:columns=["Brand","Price"]with a dict:columns={"Brand":"string","Price":"int"}This will not only check that the specified columns are found from the DataFrame but also that theirdtypeis the
expected. In case of a wrongdtype, an error message similar to following will explain the mismatch:AssertionError("Column Price has wrong dtype. Was int, expected double")You can enable strict-mode for both@df_inand@df_out. This will raise an error if the DataFrame contains columns
not defined in the annotation:@df_in(columns=["Brand"],strict=True)defprocess_cars(car_df):# do stuff with carswill raise an error whencar_dfcontains columns["Brand", "Price"]:AssertionError:DataFramecontainedunexpectedcolumn(s):PriceTo quickly check what the incoming and outgoing dataframes contain, you can add a@df_logannotation to the function.
For example adding@df_logto the abovefilter_carsfunction will product log lines:Functionfilter_carsparameterscontainedaDataFrame:columns:['Brand','Price']Functionfilter_carsreturnedaDataFrame:columns:['Brand','Price']or with@df_log(include_dtypes=True)you get:Functionfilter_carsparameterscontainedaDataFrame:columns:['Brand','Price']withdtypes['object','int64']Functionfilter_carsreturnedaDataFrame:columns:['Brand','Price']withdtypes['object','int64']Note:@df_log(include_dtypes=True)also takes thenameparameter likedf_infor the multi-param functions validationContributingContributions are accepted. Include tests in PR's.DevelopmentTo run the tests, clone the repository, install dependencies withpipand run tests withPyTest:python-mpytest--import-mode=appendtests/LicenseBSD 3-Clause License