-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Diff tables' individual values instead of by row * Add a simple notebook that actually runs some diffs. * Add empty-case test + some docs in notebook. --------- Co-authored-by: Zane Selvans <[email protected]>
- Loading branch information
1 parent
f020a07
commit 4f6b8dc
Showing
3 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Example of diffing tables across multiple different SQLite DBs.\n", | ||
"\n", | ||
"The tables must have the same name/schema. This is intended for use in\n", | ||
"investigating validation test errors.\n", | ||
"\"\"\"\n", | ||
"import sqlite3\n", | ||
"from pathlib import Path\n", | ||
"from typing import Iterable\n", | ||
"\n", | ||
"import pandas as pd\n", | ||
"\n", | ||
"from pudl.helpers import diff_wide_tables, TableDiff\n", | ||
"from pudl.metadata.classes import Resource\n", | ||
"from pudl.metadata.fields import apply_pudl_dtypes\n", | ||
"\n", | ||
"\n", | ||
"def table_diff(\n", | ||
" table_name: str,\n", | ||
" old_conn,\n", | ||
" new_conn,\n", | ||
" ignore_cols: Iterable[str] = (\"plant_id_ferc1\",),\n", | ||
" addl_key_cols: Iterable[str] = (),\n", | ||
" ) -> TableDiff:\n", | ||
" \"\"\"Diff two versions of the same table that live in SQL databases.\n", | ||
"\n", | ||
" The table has to have the same name + columns in both DBs.\n", | ||
"\n", | ||
" Args:\n", | ||
" table_name: the name, in the SQL database, of the table you want to compare.\n", | ||
" old_conn: SQLite connection to the old version of the database.\n", | ||
" new_conn: SQLite connection to the new version of the database.\n", | ||
" ignore_cols: a list of columns that you would like to ignore diffs in.\n", | ||
" addl_key_cols: \n", | ||
" columns that aren't necessarily in the primary key, but that you'd\n", | ||
" like to use as key columns for the diff - for example, if your\n", | ||
" table only uses `record_id` as primary_key, but you want to group\n", | ||
" the rows by `record_year` and `utility_id` as well, you would pass\n", | ||
" those in.\n", | ||
" \"\"\"\n", | ||
" query = f\"SELECT * FROM {table_name}\" # noqa: S608\n", | ||
" old_table = apply_pudl_dtypes(pd.read_sql(query, old_conn))\n", | ||
" new_table = apply_pudl_dtypes(pd.read_sql(query, new_conn))\n", | ||
"\n", | ||
" cols = list(set(old_table.columns) - set(ignore_cols))\n", | ||
"\n", | ||
" primary_key = list(set(Resource.from_id(table_name).schema.primary_key).union(set(addl_key_cols)))\n", | ||
" return diff_wide_tables(primary_key, old_table[cols], new_table[cols])\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"new_db = sqlite3.connect(Path(\"~/Downloads/pudl.sqlite\").expanduser())\n", | ||
"old_db = sqlite3.connect(Path(\"~/Downloads/pudl (2).sqlite\").expanduser())\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"table_name = \"denorm_plants_steam_ferc1\"\n", | ||
"diff = table_diff(table_name, old_db, new_db, ignore_cols=(\"plant_id_ferc1\", \"plant_id_pudl\"), addl_key_cols=(\"report_year\", \"utility_id_pudl\"))\n", | ||
"diff.changed" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "pudl-dev", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters