← Back to case study

PROJECT 013 · PYTHON SOURCE

Missing rental-hour coverage

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 13, 'dataset': 'bikes', 'title': 'Missing rental-hour coverage', 'question': 'When are calendar hours missing from the rental dataset?', 'method': 'Reindex to a complete hourly timeline and audit monthly observation coverage.', 'action': 'Clarify whether gaps reflect outages, suppressed zero counts or missing ingestion before training.', 'limitations': 'Historical system rentals measure realized use, not unmet demand or station inventory. Missing hours are unknown. Weather associations do not establish causal effects. '}

def analyze():
    df = load('bikes').copy()
    series=df.set_index('timestamp').cnt.reindex(pd.date_range(df.timestamp.min(),df.timestamp.max(),freq='h'))
    audit=pd.DataFrame({'missing':series.isna().astype(int),'month':series.index.strftime('%Y-%m')})
    t=audit.groupby('month').missing.agg(calendar_hours='size',missing_hours='sum').reset_index();t['missing_share']=t.missing_hours/t.calendar_hours
    out=result(t,'month','missing_share','Missing-hour share','Observed records remain distinct from the complete calendar; absent counts are never filled with zero.')
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)