{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-30T02:22:39.094503Z",
     "start_time": "2024-12-30T02:22:37.260375Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\n",
    "    '../temp/stock/SZ000001.csv',\n",
    "    usecols=[1, 3],\n",
    "    names=['ds', 'y'],\n",
    ")\n",
    "df['ds'] = pd.to_datetime(df['ds'])\n",
    "# 筛选2023年前的数据放在df_train变量中\n",
    "df_train = df[(df['ds'] < '2024-01-01')]\n",
    "# 筛选2024年的数据放在df_test变量中\n",
    "df_test = df[df['ds'] >= '2024-01-01']\n",
    "df_test.info()"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 240 entries, 7824 to 8063\n",
      "Data columns (total 2 columns):\n",
      " #   Column  Non-Null Count  Dtype         \n",
      "---  ------  --------------  -----         \n",
      " 0   ds      240 non-null    datetime64[ns]\n",
      " 1   y       240 non-null    float64       \n",
      "dtypes: datetime64[ns](1), float64(1)\n",
      "memory usage: 5.6 KB\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:22:42.993258Z",
     "start_time": "2024-12-30T02:22:39.096297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from prophet import Prophet\n",
    "\n",
    "model = Prophet()\n",
    "model.fit(df_train)\n",
    "predict = model.predict(df_test)\n",
    "predict.info()"
   ],
   "id": "640e09eb5973328f",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Importing plotly failed. Interactive plots will not work.\n",
      "10:22:41 - cmdstanpy - INFO - Chain [1] start processing\n",
      "10:22:42 - cmdstanpy - INFO - Chain [1] done processing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 240 entries, 0 to 239\n",
      "Data columns (total 19 columns):\n",
      " #   Column                      Non-Null Count  Dtype         \n",
      "---  ------                      --------------  -----         \n",
      " 0   ds                          240 non-null    datetime64[ns]\n",
      " 1   trend                       240 non-null    float64       \n",
      " 2   yhat_lower                  240 non-null    float64       \n",
      " 3   yhat_upper                  240 non-null    float64       \n",
      " 4   trend_lower                 240 non-null    float64       \n",
      " 5   trend_upper                 240 non-null    float64       \n",
      " 6   additive_terms              240 non-null    float64       \n",
      " 7   additive_terms_lower        240 non-null    float64       \n",
      " 8   additive_terms_upper        240 non-null    float64       \n",
      " 9   weekly                      240 non-null    float64       \n",
      " 10  weekly_lower                240 non-null    float64       \n",
      " 11  weekly_upper                240 non-null    float64       \n",
      " 12  yearly                      240 non-null    float64       \n",
      " 13  yearly_lower                240 non-null    float64       \n",
      " 14  yearly_upper                240 non-null    float64       \n",
      " 15  multiplicative_terms        240 non-null    float64       \n",
      " 16  multiplicative_terms_lower  240 non-null    float64       \n",
      " 17  multiplicative_terms_upper  240 non-null    float64       \n",
      " 18  yhat                        240 non-null    float64       \n",
      "dtypes: datetime64[ns](1), float64(18)\n",
      "memory usage: 35.8 KB\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:22:43.306603Z",
     "start_time": "2024-12-30T02:22:42.995547Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_show = pd.concat([df_test[['ds', 'y']].set_index('ds'), predict[['ds', 'yhat']].set_index('ds').round(2)], axis=1)\n",
    "df_show.plot(figsize=(10, 5))"
   ],
   "id": "d44a228e2358b7c9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='ds'>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ],
      "image/png": "[encoded data, 51588 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
