-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple notebook that actually runs some diffs.
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
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,91 @@ | ||
{ | ||
"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", | ||
"\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", | ||
" query = f\"SELECT * FROM {table_name}\"\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