Skip to content

Instantly share code, notes, and snippets.

@0187773933
Created April 20, 2026 16:24
Show Gist options
  • Select an option

  • Save 0187773933/e181329910111228bbcca9470969a18e to your computer and use it in GitHub Desktop.

Select an option

Save 0187773933/e181329910111228bbcca9470969a18e to your computer and use it in GitHub Desktop.
Technology Adoption
#!/usr/bin/env python3
import requests
import pandas as pd
import numpy as np
from io import StringIO
URL = "https://ourworldindata.org/grapher/technology-adoption-by-households-in-the-united-states.csv"
print("Downloading dataset...")
resp = requests.get(URL)
resp.raise_for_status()
df = pd.read_csv(StringIO(resp.text))
# convert to long if needed
if "Entity" not in df.columns:
df = df.melt(id_vars=["Year"], var_name="Entity", value_name="Value")
df = df.rename(columns={
"Technology Diffusion (Comin and Hobijn (2004) and others)": "Value"
})
results = []
for entity, sub in df.groupby("Entity"):
sub = sub.dropna().sort_values("Year")
if len(sub) < 5:
continue
t = sub["Year"].values
y = sub["Value"].values
# normalize (optional)
if np.max(y) < 20:
continue
# compute slope (dy/dt)
dy = np.gradient(y, t)
# smooth a bit to avoid noise spikes
dy_smooth = pd.Series(dy).rolling(3, center=True).mean().to_numpy()
if np.all(np.isnan(dy_smooth)):
continue
idx = np.nanargmax(dy_smooth)
midpoint_year = t[idx]
slope = dy_smooth[idx]
results.append((entity, midpoint_year, slope))
results.sort(key=lambda x: x[1])
print("\n=== MIDPOINT YEARS (max slope) ===\n")
for entity, year, slope in results:
print(f"{entity:35s} midpoint ≈ {year:.0f} (slope={slope:.2f})")
print("\nDone.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment