<jupyter_start><jupyter_text>YData Quality - Data Expectations TutorialTime-to-Value: 8 minutesThis notebook provides a tutorial for the ydata_quality package integration of the Great Expectations library for managing data expectations.**Structure:**1. A data expectations introduction2. Load example dataset3. Instantiate the Data Quality engine4. Run the quality checks5. Assess the warnings6. (Extra) Detailed overview  A data expectations introduction What are data expectations?Detecting inconsistencies or even errors in data can sometimes be a trivial task, but surely this is far from being the norm. Many times this task requires minucious inspection of lots of data structures or advanced domain knowledge that allows a user to confidently label any shortcoming.Consider __[Test-Driven-Development](https://en.wikipedia.org/wiki/Test-driven_development)__ (TDD) for a moment. In a TDD process, software requirements are realized into test cases before the development of the software itself. Software changes are constantly ran against these test cases in order to, hopefully, detect any sort of problem that might occur. A full software pipeline can be tested in this fashion to establish a quality assurance protocol, warrant a green light for a production push, and supporting refactorizations.But what about data? What if you could generalize domain knowledge, and generally expected data behaviour, into the datasets you manipulate, either internally sourced or from third parties? In fact many teams already do this in one way or another, but generally resorting to ad-hoc and hard to generalize processes. Taking the lesson from TDD, if we could develop a set of verifiable tests that work just like software test cases we would also get the same benefits.**Data Expectation** is the name we use for unit tests applied to data, to define an expectation about data is to develop a unit test that asserts a certain property about the data and provides an actionable output in any deviation. What is Great Expectations?__[Great Expectations](https://greatexpectations.io/)__ is a Python tool for creating and running data expectations suite, allowing you to validate, profile your data, automate report creation in the form of HTML documents and store validation logs. Great Expectations offers a wide range of built-in expectations but also allows you to define custom expectations that better fit to your needs. How can I leverage my Great Expectations project with YData Quality?It's simple!1. Locate the validations directory of your Great Expectations project, which should be under the *uncommitted* directory. There you will find a set of folders, one for each validation run that you executed.2. Choose a validation run to which you would like to get more insight, and copy the path to the json file.3. Instantiate a DataExpectationsReporter engine and run evaluate by providing the json file path.Congratulations you are all set!<jupyter_code>import pandas as pd

from ydata_quality.data_expectations import DataExpectationsReporter<jupyter_output><empty_output><jupyter_text>Load the example dataset and path to Great Expectations validation runWe will use a demo from the GE tutorials. The taxi ride dataset and a log from a validation run on this dataset.<jupyter_code># This is the DataFrame used in the demo from GE tutorials
df = pd.read_csv('../datasets/original/taxi_yellow_tripdata_sample_2019-01.csv')

# This is a sample json log taken from a validation run
results_json_path = '../datasets/original/taxi_long.json'<jupyter_output><empty_output><jupyter_text>Create the engineEach engine contains the checks and tests for each suite.<jupyter_code>der = DataExpectationsReporter()<jupyter_output><empty_output><jupyter_text>Full EvaluationThe easiest way to assess the data quality analysis is to run `.evaluate()` which returns a dictionary with outputs of operation performed.<jupyter_code>results = der.evaluate(results_json_path, df)<jupyter_output>[1mWarnings:[0m
	TOTAL: 9 warning(s)
	[1m[38;5;11mPriority 2[0m: 2 warning(s)
	[1m[38;5;69mPriority 3[0m: 7 warning(s)


[38;5;11m[1mPriority 2[0m - [1musage allowed, limited human intelligibility[0m:
	[38;5;11m*[0m [1m[DATA EXPECTATIONS[0m - [4mOVERALL ASSESSMENT][0m 10 expectations have failed, which is more than the implied absolute threshold of 0 failed expectations.
	[38;5;11m*[0m [1m[DATA EXPECTATIONS[0m - [4mCOVERAGE FRACTION][0m The provided DataFrame has a total expectation coverage of 11% of its columns, which is below the expected coverage of 75%.
[38;5;69m[1mPriority 3[0m - [1mminor impact, aesthetic[0m:
	[38;5;69m*[0m [1m[DATA EXPECTATIONS[0m - [4mEXPECTATION ASSESSMENT - VALUE BETWEEN][0m Column passenger_count - The observed value is outside of the expected range.
	- The observed value is -100% deviated from the nearest bound of the expected range.
	[38;5;69m*[0m [1m[DATA EXPECTATIONS[0m - [4mEXPECTATION ASSESSMENT - VALUE BETWEEN[...]<jupyter_text>Check the statusAfter running the data quality checks, you can check the warnings for each individual operation over the GE validation log. The warnings are sorted by priority and have additional details that can provide better insights for Data Scientists.<jupyter_code>warnings = der.get_warnings()
warnings[0]<jupyter_output><empty_output><jupyter_text>Full Test SuiteIn this section, you will find a detailed overview of the available tests in the data expectations module of ydata_quality. These are all run with the `evaluate` method, which centralizes input arguments and produces specific outputs in the returned results dictionary, structured by test.<jupyter_code># Results object structure
list(results.keys())<jupyter_output><empty_output><jupyter_text>Overall assessmentThis method controls for errors in the expectation suite level.It receives your results_json_path and 2 optional arguments.The default is a 0 failed expectations tolerance. You can configure this threshold using one of two arguments:1. An integer for the maximum number of expectations you tolerate as failures, error_tolor2. The fraction of expectations you tolerate as failures, rel_error_tolAny number of failed expectations greater than the one implied by these arguments will store a warning that will be part of the report. The data object of this warning consists in a dictionary with a value consisting in the list of indexes of the failed expectations, according to your expectation suite. The same list is stored in the returned results of `evaluate`. Note that the IDs are not arbitrary, they obey the order implied by your expectation suite, according to zero-based numbering.<jupyter_code>failed_expectations_ids = results['Overall Assessment']
failed_expectations_ids<jupyter_output><empty_output><jupyter_text>Coverage fractionThis method controls for total expectation coverage of your dataset.It receives your results_json_path, the dataset for which you ran your validation and 1 optional argument.By default the engine expects a minimum coverage of 70% of your columns by the expectation suite. Coverage is considered only in specific column expectations, table expectations like __[the list of table expectations](https://docs.greatexpectations.io/docs/reference/glossary_of_expectationstable-shape)__ are not considered here. You can callibrate your desired minimum coverage by providing the argument minimum_coverage as a fraction.Any coverage inferior to the one implied by these arguments will store a warning that will be part of the report. The data object of this warning is the set of columns of your dataset that are not covered by the expectation suite. The method stores in the results object the fraction of columns of your dataset that are covered by at least one expectation.Additionally, if there are any expectations in your expectation suite meant for a column which cannot be found on the provided dataset, an exception will be raised which will be captured by the `evaluate` method.<jupyter_code>coverage_fraction = results['Coverage Fraction']
coverage_fraction<jupyter_output><empty_output><jupyter_text>Expectation level assessmentThis method checks the success of your expectation suite at the expectation level.It receives one argument, your results_json_path.It stores no warnings directly but depending on the failing expectations, it may call private methods to further digest the stored information. These expectation specific methods can store warnings for you to have some additional insight into what is wrong.After running, a tuple will be returned with the following contents:1. A report containing the status for all your expectations, succesful or not and with additional information on the failed expectations. Further information for interpreting the error metrics is provided in the stored warnings description.2. A dense representation of the expectations, including:     * Results format;    * Success status;    * Expectation type;    * Flag indicating if it is a table expectation;    * General kwargs;    * Column kwargs.<jupyter_code>expectations_report, expectations_dense = results['Expectation Level Assessment']
expectations_report
# Retrieve a dense representation of an expectation
expectations_dense[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.050801851253901624, 'fraction_numerical': 0.012592831772683243, 'mean_word_length': 5.777534646243618, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '478173', 'license': 'MIT', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2202, 'n_words': 1185, 'path': '4 - data-quality-inspection/data_expectations.ipynb', 'repo_name': 'ydataai/Blog', 'size': '9291'}