Skip to content

Commit

Permalink
Merge pull request #9245 from bashtage/pandas-3-compat
Browse files Browse the repository at this point in the history
MAINT: Fix issues with pandas 3
  • Loading branch information
bashtage committed May 14, 2024
2 parents aa3c42e + 8ba53f9 commit ba1ffab
Show file tree
Hide file tree
Showing 23 changed files with 345 additions and 329 deletions.
112 changes: 56 additions & 56 deletions examples/notebooks/autoregressions.ipynb

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions examples/notebooks/deterministics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"plt.rc(\"figure\", figsize=(16, 9))\n",
"plt.rc(\"font\", size=16)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -38,14 +38,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.deterministic import DeterministicProcess\n",
"\n",
"index = pd.RangeIndex(0, 100)\n",
"det_proc = DeterministicProcess(index, constant=True, order=1, seasonal=True, period=5)\n",
"det_proc.in_sample()"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -58,10 +58,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.out_of_sample(15)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -79,10 +79,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.range(190, 210)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -97,21 +97,21 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"index = pd.period_range(\"2020-03-01\", freq=\"M\", periods=60)\n",
"det_proc = DeterministicProcess(index, constant=True, fourier=2)\n",
"det_proc.in_sample().head(12)"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.out_of_sample(12)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -124,10 +124,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.range(\"2025-01\", \"2026-01\")"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -140,10 +140,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.range(58, 70)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -158,7 +158,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.deterministic import Fourier, Seasonality, TimeTrend\n",
"\n",
Expand All @@ -168,7 +167,8 @@
"seas = Seasonality(period=7)\n",
"det_proc = DeterministicProcess(index, additional_terms=[tt, seas, four])\n",
"det_proc.in_sample().head(28)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -185,7 +185,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.deterministic import DeterministicTerm\n",
"\n",
Expand Down Expand Up @@ -220,20 +219,21 @@
" return pd.DataFrame(\n",
" terms, columns=[\"const_break\", \"trend_break\"], index=fcast_index\n",
" )"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"btt = BrokenTimeTrend(60)\n",
"tt = TimeTrend(constant=True, order=1)\n",
"index = pd.RangeIndex(100)\n",
"det_proc = DeterministicProcess(index, additional_terms=[tt, btt])\n",
"det_proc.range(55, 65)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -246,7 +246,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class ExogenousProcess(DeterministicTerm):\n",
" def __init__(self, data):\n",
Expand All @@ -266,51 +265,52 @@
" ):\n",
" forecast_index = self._extend_index(index, steps, forecast_index)\n",
" return self._data.loc[forecast_index]"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"gen = np.random.default_rng(98765432101234567890)\n",
"exog = pd.DataFrame(gen.integers(100, size=(300, 2)), columns=[\"exog1\", \"exog2\"])\n",
"exog.head()"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ep = ExogenousProcess(exog)\n",
"tt = TimeTrend(constant=True, order=1)\n",
"# The in-sample index\n",
"idx = exog.index[:200]\n",
"det_proc = DeterministicProcess(idx, additional_terms=[tt, ep])"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.in_sample().head()"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_proc.out_of_sample(10)"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -336,7 +336,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gen = np.random.default_rng(98765432101234567890)\n",
"idx = pd.RangeIndex(200)\n",
Expand All @@ -350,7 +349,8 @@
" y[i] = 0.9 * y[i - 1] + det_terms[i] @ params + gen.standard_normal()\n",
"y = pd.Series(y, index=idx)\n",
"ax = y.plot()"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -363,14 +363,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.api import AutoReg\n",
"\n",
"mod = AutoReg(y, 1, trend=\"n\", deterministic=det_proc)\n",
"res = mod.fit()\n",
"print(res.summary())"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -383,20 +383,20 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = res.plot_predict(200, 200 + 2 * 52, True)"
]
],
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"auto_reg_forecast = res.predict(200, 211)\n",
"auto_reg_forecast"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -428,7 +428,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.api import SARIMAX\n",
"\n",
Expand All @@ -438,7 +437,8 @@
"mod = SARIMAX(y, order=(1, 0, 0), trend=\"c\", exog=det_terms)\n",
"res = mod.fit(disp=False)\n",
"print(res.summary())"
]
],
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -451,13 +451,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sarimax_forecast = res.forecast(12, exog=det_proc.out_of_sample(12))\n",
"df = pd.concat([auto_reg_forecast, sarimax_forecast], axis=1)\n",
"df.columns = columns = [\"AutoReg\", \"SARIMAX\"]\n",
"df"
]
],
"outputs": []
}
],
"metadata": {
Expand Down

0 comments on commit ba1ffab

Please sign in to comment.