Count rows, keys and observed durations
Use these three controls when comparing the SQLite and pandas imports of orders.csv.
- All rows
96 records - Distinct order IDs
96 records - Nonblank delivery days
92 records
Check your reading
The keys are unique. Four durations are missing. COUNT(days_to_deliver) in SQLite and Series.count() in pandas should both return 92 after importing those blanks as missing values.
Open the case files, data dictionary and portfolio challenge
Create a project folder with a data subfolder and put the downloaded CSVs there. Open a terminal in the project folder. Use python -m venv .venv and the environment’s Python to install pandas. Save the interpreter and pandas versions in your handover. The database in this course runs inside Python using sqlite3.
Treat identifiers as text even when they contain digits. A missing elapsed time has a different meaning from zero elapsed days. Parse the date with the expected ISO layout and raise an error on invalid input. Before calculation, write down what one row represents, which column must be unique and which columns permit blanks.
Start a file called report.py with the following. Run it from this folder after each addition. Keep the original files intact.
from pathlib import Path
import sqlite3
import pandas as pd
data = Path("data")
def read_orders(filename):
frame = pd.read_csv(data / filename, dtype={"order_id": "string", "product_id": "string", "branch": "string", "days_to_deliver": "Int64"})
frame["order_date"] = pd.to_datetime(frame["order_date"], format="%Y-%m-%d", errors="raise")
return frame
orders = read_orders("orders.csv")
products = pd.read_csv(data / "products.csv", dtype="string")
targets = pd.read_csv(data / "targets.csv", parse_dates=["month"])
print(orders.shape, orders["order_id"].nunique(), orders.isna().sum())