|
| 1 | +Getting Started |
| 2 | +=============== |
| 3 | + |
| 4 | +Welcome to DataFrame Expectations! This guide will help you get up and running quickly with validating your Pandas and PySpark DataFrames. |
| 5 | + |
| 6 | +Installation |
| 7 | +------------ |
| 8 | + |
| 9 | +Install DataFrame Expectations using pip: |
| 10 | + |
| 11 | +.. code-block:: bash |
| 12 | +
|
| 13 | + pip install dataframe-expectations |
| 14 | +
|
| 15 | +Requirements |
| 16 | +~~~~~~~~~~~~ |
| 17 | + |
| 18 | +* Python 3.10+ |
| 19 | +* pandas >= 1.5.0 |
| 20 | +* pyspark >= 3.3.0 |
| 21 | +* tabulate >= 0.8.9 |
| 22 | + |
| 23 | +Basic Usage |
| 24 | +----------- |
| 25 | + |
| 26 | +DataFrame Expectations provides a fluent API for building validation suites. Here's how to get started: |
| 27 | + |
| 28 | +Pandas Example |
| 29 | +~~~~~~~~~~~~~~ |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + import pandas as pd |
| 34 | + from dataframe_expectations.expectations_suite import DataframeExpectationsSuite |
| 35 | +
|
| 36 | + # Create a sample DataFrame |
| 37 | + df = pd.DataFrame({ |
| 38 | + "age": [25, 15, 45, 22], |
| 39 | + "name": ["Alice", "Bob", "Charlie", "Diana"], |
| 40 | + "salary": [50000, 60000, 80000, 45000] |
| 41 | + }) |
| 42 | +
|
| 43 | + # Build a validation suite |
| 44 | + suite = ( |
| 45 | + DataframeExpectationsSuite() |
| 46 | + .expect_min_rows(3) # At least 3 rows |
| 47 | + .expect_max_rows(10) # At most 10 rows |
| 48 | + .expect_value_greater_than("age", 18) # All ages > 18 |
| 49 | + .expect_value_less_than("salary", 100000) # All salaries < 100k |
| 50 | + .expect_value_not_null("name") # No null names |
| 51 | + ) |
| 52 | +
|
| 53 | + # Run validation |
| 54 | + suite.run(df) |
| 55 | +
|
| 56 | +
|
| 57 | +PySpark Example |
| 58 | +~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + from pyspark.sql import SparkSession |
| 63 | + from dataframe_expectations.expectations_suite import DataframeExpectationsSuite |
| 64 | +
|
| 65 | + # Initialize Spark |
| 66 | + spark = SparkSession.builder.appName("DataFrameExpectations").getOrCreate() |
| 67 | +
|
| 68 | + # Create a sample DataFrame |
| 69 | + data = [ |
| 70 | + {"age": 25, "name": "Alice", "salary": 50000}, |
| 71 | + {"age": 15, "name": "Bob", "salary": 60000}, |
| 72 | + {"age": 45, "name": "Charlie", "salary": 80000}, |
| 73 | + {"age": 22, "name": "Diana", "salary": 45000} |
| 74 | + ] |
| 75 | + df = spark.createDataFrame(data) |
| 76 | +
|
| 77 | + # Build a validation suite (same API as Pandas!) |
| 78 | + suite = ( |
| 79 | + DataframeExpectationsSuite() |
| 80 | + .expect_min_rows(3) |
| 81 | + .expect_max_rows(10) |
| 82 | + .expect_value_greater_than("age", 18) |
| 83 | + .expect_value_less_than("salary", 100000) |
| 84 | + .expect_value_not_null("name") |
| 85 | + ) |
| 86 | +
|
| 87 | + # Run validation |
| 88 | + suite.run(df) |
| 89 | +
|
| 90 | +Example Output |
| 91 | +~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +When validations fail, you'll see detailed output like this: |
| 94 | + |
| 95 | +.. code-block:: text |
| 96 | +
|
| 97 | + ========================== Running expectations suite ========================== |
| 98 | + ExpectationMinRows (DataFrame contains at least 3 rows) ... OK |
| 99 | + ExpectationMaxRows (DataFrame contains at most 10 rows) ... OK |
| 100 | + ExpectationValueGreaterThan ('age' is greater than 18) ... FAIL |
| 101 | + ExpectationValueLessThan ('salary' is less than 100000) ... OK |
| 102 | + ExpectationValueNotNull ('name' is not null) ... OK |
| 103 | + ============================ 4 success, 1 failures ============================= |
| 104 | +
|
| 105 | + ExpectationSuiteFailure: (1/5) expectations failed. |
| 106 | +
|
| 107 | + ================================================================================ |
| 108 | + List of violations: |
| 109 | + -------------------------------------------------------------------------------- |
| 110 | + [Failed 1/1] ExpectationValueGreaterThan ('age' is greater than 18): Found 1 row(s) where 'age' is not greater than 18. |
| 111 | + Some examples of violations: |
| 112 | + +-----+------+--------+ |
| 113 | + | age | name | salary | |
| 114 | + +-----+------+--------+ |
| 115 | + | 15 | Bob | 60000 | |
| 116 | + +-----+------+--------+ |
| 117 | + ================================================================================ |
| 118 | +
|
| 119 | +How to contribute? |
| 120 | +------------------ |
| 121 | +Contributions are welcome! You can enhance the library by adding new expectations, refining existing ones, or improving |
| 122 | +the testing framework or the documentation. |
0 commit comments