How to Use Great Expectations: A Beginner’s Guide to Data Validation Fundamentals

How to Use Great Expectations: A Beginner's Guide to Data Validation Fundamentals

Great Expectations is a powerful data validation framework, but like many powerful tools, it’s best to start with the fundamentals. Let’s break down the core concepts in a way that makes them approachable and practical.

What is Great Expectations?

Think of Great Expectations as a friendly data quality assistant. Just as you might proofread a document before sending it, Great Expectations helps you check your data before using it. It ensures your data meets the standards you need, catching problems early before they cause issues downstream in your analysis or applications.

Installing Great Expectations

Great Expectations installation follows best practices for Python package management. While you can install it directly into your base Python environment, using a virtual or distinct environment helps keep your projects organized and prevents dependency conflicts. Once your environment is activated, install Great Expectations using pip, the Python package installer. While Great Expectations is available through both pip and conda, the pip installation is recommended as it provides the most up-to-date version and better dependency resolution:

 
# Install Great Expectations
pip install great-expectations

With Great Expectations installed and ready to go, you can start exploring its powerful features for data validation and quality checking. For a detailed guide, please visit the official documentation. 

The Three Core Pillars

Let’s explore the three fundamental concepts that form the foundation of Great Expectations. I’ll break down our example code to show how each piece works in practice.

1. Data Assets: Your Data’s Home Base

Data Assets are simply containers for your data. They can be anything from a simple CSV file to a complex database table. Here’s how we set up a Data Asset in our example:

 
# Import Necessary Libraries
import great_expectations as gx
import pandas as pd

# Create a simple dataset
data = {
    'age': [25, 30, -5, 45, 150]
}
df = pd.DataFrame(data)

# Set up Great Expectations with this data
context = gx.get_context()
data_source = context.data_sources.add_pandas(name="my_datasource")
data_asset = data_source.add_dataframe_asset(name="my_dataframe")
batch_definition = data_asset.add_batch_definition_whole_dataframe(
    "my_batch_definition"
)
batch = batch_definition.get_batch(batch_parameters={"dataframe": df})

This code shows how we create a simple pandas DataFrame and register it with Great Expectations. Think of this process like introducing your data to Great Expectations – you’re saying “Here’s my data, and this is how I want you to handle it.”

When working with Great Expectations, setting up your data involves several important steps that create what’s called a “data context”. Let’s examine each step in detail:

1. Creating the Context (context = gx.get_context()):

  • This is like opening a new workspace in Great Expectations
  • The context manages all your data sources, expectations, and validation results
  • It’s the central hub that coordinates all Great Expectations operations

2. Adding a Data Source (data_source = context.data_sources.add_pandas(name=”my_datasource”)):

  • This tells Great Expectations we want to work with a pandas DataFrame
  • The name “my_datasource” is a unique identifier for this data source
  • You can have multiple data sources (like databases, CSV files, or other DataFrames)

3. Creating a Data Asset (data_asset = data_source.add_dataframe_asset(name=”my_dataframe”)):

  • This defines how we want to interact with our specific DataFrame
  • Think of it as registering our DataFrame with Great Expectations
  • The name “my_dataframe” helps us reference this specific dataset later

4. Defining the Batch (batch_definition = data_asset.add_batch_definition_whole_dataframe(“my_batch_definition”)):

  • This specifies how we want to process our data
  • “whole_dataframe” means we’re working with the entire DataFrame at once
  • You could alternatively process data in smaller chunks or batches

5. Getting the Batch (batch = batch_definition.get_batch(batch_parameters={“dataframe”: df})):

  • This is where we actually connect our pandas DataFrame (`df`) to Great Expectations
  • The batch is what we’ll use to run our validations
  • The `batch_parameters` tell Great Expectations which DataFrame to use

This infrastructure might seem complex at first, but it serves an important purpose: it creates a structured environment where Great Expectations can track and manage your data validation process. Think of it as setting up a specialized laboratory where you can run experiments (validations) on your data. Each step builds upon the previous one, creating a complete system for data validation.

Once this setup is complete, your data is ready for validation, and you can start applying expectations to check its quality. This structured approach helps ensure consistent and reliable data validation across your entire project.

2. Expectations: Setting the Rules

Expectations are the rules you want your data to follow. They’re like a checklist for your data. In our example, we create a simple but practical expectation:

 
# Add a simple expectation
expectation = gx.expectations.ExpectColumnValuesToBeBetween(
    column="age", min_value=0, max_value=120
)

This expectation is straightforward: we’re saying that all values in the ‘age’ column should be between 0 and 120. This makes intuitive sense – we wouldn’t expect to see negative ages or ages over 120 in our dataset. Great Expectations comes with many built-in expectations like this one, each designed to check different aspects of your data.

3. Validation: Checking the Results

Validation is where we actually test our data against our expectations. It’s the moment of truth:

 
# Check if data meets our expectations
results = batch.validate(expectation)

# Print results
print("\nValidation Results:")
print("==================")
print(f"Passed: {results.success}")
if not results.success:
    failed_values = results.result['partial_unexpected_list']
    print(f"Failed values: {failed_values}")

When we run this code, we get the following output:

 
Calculating Metrics: 100%|█████████████████████████████████████████| 10/10 [00:00<00:00, 399.73it/s]

Validation Results:
==================
Passed: False
Failed values: [-5, 150]

The validation failed because we have two values that don’t meet our expectations: -5 (below 0) and 150 (above 120). Great Expectations not only tells us that the validation failed but also shows us exactly which values caused the failure. This is incredibly useful for debugging and fixing data quality issues.

Putting It All Together

The beauty of Great Expectations lies in how these three concepts work together. You start with your data (Data Asset), define what you expect from it (Expectations), and then check if those expectations are met (Validation). It’s like having a conversation with your data:

1. “Here’s my dataset” (Data Asset)
2. “Here’s what I expect to see in it” (Expectations)
3. “Now, let’s check if everything looks right” (Validation)

When you’re starting with Great Expectations, focus on mastering these three concepts. Once you’re comfortable with them, you can explore more advanced features like automated data documentation, profiling, and complex validation pipelines. The goal isn’t to create perfect data – it’s to understand your data and catch potential issues before they become problems. Starting small, perhaps with just one or two simple expectations, provides a solid foundation for building more complex data quality checks as your needs grow.

Leave a Reply

Your email address will not be published. Required fields are marked *