Skip to content

Instantly share code, notes, and snippets.

@ian-whitestone
Created January 12, 2020 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-whitestone/857330161a57521145e0e4097ae9b04c to your computer and use it in GitHub Desktop.
Save ian-whitestone/857330161a57521145e0e4097ae9b04c to your computer and use it in GitHub Desktop.
Quickstart examples for getting up and running with great expectations
## Pandas
import great_expectations as ge
# Build up expectations on a sample dataset and save them
train = ge.read_csv("data/npi.csv")
train.expect_column_values_to_not_be_null("NPI")
train.save_expectation_suite("npi_csv_expectations.json")
# Load in a new dataset and test them
test = ge.read_csv("data/npi_new.csv")
validation_results = test.validate(expectation_suite="npi_csv_expectations.json")
if validation_results["success"]:
print ("giddy up!")
else:
raise Exception("oh shit.")
## SQLAlchemy
import os
from great_expectations.dataset import SqlAlchemyDataset
from sqlalchemy import create_engine
db_string = "postgres://{user}:{password}@{host}:{port}/{dbname}".format(
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"],
port=os.environ["DB_PORT"],
dbname=os.environ["DB_DBNAME"],
host=os.environ["DB_HOST"],
)
db_engine = create_engine(db_string)
# Build up expectations on a table and save them
sql_dataset = SqlAlchemyDataset(table_name='my_table', engine=db_engine)
sql_dataset.expect_column_values_to_not_be_null("id")
sql_dataset.save_expectation_suite("postgres_expectations.json")
# Load in a subset of the table and test it
sql_query = """
select *
from my_table
where created_at between date'2019-11-07' and date'2019-11-08'
"""
new_sql_dataset = SqlAlchemyDataset(custom_sql=sql_query, engine=db_engine)
validation_results = new_sql_dataset.validate(
expectation_suite="postgres_expectations.json"
)
if validation_results["success"]:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment