4 definitions in keeling-curve.py that other code can import and call. Each has the notebook's setup cell, and nothing else of it.
def load_co2(path: str = DATA) -> pd.DataFrame
Monthly mean CO₂ in the air at Mauna Loa, Hawaii, in ppm, from March 1958.
Columns: year, month, decimal_date, average, deseasonalized, ndays, sdev,
unc. NOAA fills months without measurements by interpolation and marks
them with a negative ndays.
| Parameter | Type | Default |
|---|---|---|
| path | str | DATA |
keeling_curve.load_co2()
Source
def load_co2(path: str = DATA) -> pd.DataFrame:
"""Monthly mean CO₂ in the air at Mauna Loa, Hawaii, in ppm, from March 1958.
Columns: year, month, decimal_date, average, deseasonalized, ndays, sdev,
unc. NOAA fills months without measurements by interpolation and marks
them with a negative ndays.
"""
co2 = pd.read_csv(path, comment="#")
return co2.rename(columns={"decimal date": "decimal_date"})def yearly_mean(path: str = DATA) -> pd.Series
The deseasonalized concentration averaged over each complete year, in ppm, by year.
| Parameter | Type | Default |
|---|---|---|
| path | str | DATA |
keeling_curve.yearly_mean()
Source
def yearly_mean(path: str = DATA) -> pd.Series:
"""The deseasonalized concentration averaged over each complete year, in ppm, by year."""
co2 = load_co2(path)
complete = co2.groupby("year").filter(lambda year: len(year) == 12)
return complete.groupby("year")["deseasonalized"].mean()def growth_rate(start_year: int, end_year: int, path: str = DATA) -> float
Average growth of CO₂ between two complete years, in ppm a year.
>>> round(growth_rate(1960, 1970), 2)
0.88
| Parameter | Type | Default |
|---|---|---|
| start_year | int | required |
| end_year | int | required |
| path | str | DATA |
keeling_curve.growth_rate(start_year=..., end_year=...)
Source
def growth_rate(start_year: int, end_year: int, path: str = DATA) -> float:
"""Average growth of CO₂ between two complete years, in ppm a year.
>>> round(growth_rate(1960, 1970), 2)
0.88
"""
yearly = yearly_mean(path)
if start_year not in yearly.index or end_year not in yearly.index:
raise ValueError(f"Both years need twelve months of data; there are {yearly.index.min()} to {yearly.index.max()}.")
return float((yearly[end_year] - yearly[start_year]) / (end_year - start_year))def seasonal_cycle(path: str = DATA) -> pd.Series
Each month's mean departure from the deseasonalized trend, in ppm: the breath of the northern forests.
| Parameter | Type | Default |
|---|---|---|
| path | str | DATA |
keeling_curve.seasonal_cycle()
Source
def seasonal_cycle(path: str = DATA) -> pd.Series:
"""Each month's mean departure from the deseasonalized trend, in ppm: the breath of the northern forests."""
co2 = load_co2(path)
return (co2["average"] - co2["deseasonalized"]).groupby(co2["month"]).mean()