{"id":6325,"date":"2025-10-02T00:26:18","date_gmt":"2025-10-01T16:26:18","guid":{"rendered":"http:\/\/192.168.1.29\/?p=6325"},"modified":"2025-10-14T12:12:09","modified_gmt":"2025-10-14T04:12:09","slug":"python%e5%ba%93baostock%e8%8e%b7%e5%8f%96%e8%82%a1%e7%a5%a8%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=6325","title":{"rendered":"python\u5e93baostock\u83b7\u53d6\u80a1\u7968\u6570\u636e"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import baostock as bs\nimport pandas as pd\nimport numpy as np\n\n# \u767b\u5f55baostock\u7cfb\u7edf\nlg = bs.login()\nprint('\u767b\u5f55\u8fd4\u56de\u4ee3\u7801\uff1a', lg.error_code)\nprint('\u767b\u5f55\u8fd4\u56de\u4fe1\u606f\uff1a', lg.error_msg)\n\n# \u83b7\u53d6\u80a1\u7968\u6570\u636e\n# \u793a\u4f8b\uff1a\u83b7\u53d6\u4e0a\u8bc1\u6307\u6570(000001.SH)2023\u5e74\u7684\u65e5K\u7ebf\u6570\u636e\nstart_date = '2025-01-01'\nend_date = '2025-9-30'\nstock_code = 'sh.600938'\n\n# \u83b7\u53d6\u65e5K\u7ebf\u6570\u636e\nrs = bs.query_history_k_data_plus(stock_code,\n    \"date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST\",\n    start_date=start_date, end_date=end_date, frequency=\"d\", adjustflag=\"3\")\n\nprint('\u83b7\u53d6K\u7ebf\u6570\u636e\u8fd4\u56de\u4ee3\u7801\uff1a', rs.error_code)\nprint('\u83b7\u53d6K\u7ebf\u6570\u636e\u8fd4\u56de\u4fe1\u606f\uff1a', rs.error_msg)\n\n# \u89e3\u6790\u6570\u636e\u5e76\u5b58\u50a8\u5230DataFrame\ndata_list = &#91;]\nwhile (rs.error_code == '0') &amp; rs.next():\n    data_list.append(rs.get_row_data())\n\n# \u521b\u5efaDataFrame\ncolumn_names = &#91;\"date\",\"code\",\"open\",\"high\",\"low\",\"close\",\"preclose\",\"volume\",\"amount\",\"adjustflag\",\"turn\",\"tradestatus\",\"pctChg\",\"isST\"]\ndf = pd.DataFrame(data_list, columns=column_names)\n\n# \u6570\u636e\u7c7b\u578b\u8f6c\u6362 - \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u683c\u5f0f\nnumeric_columns = &#91;\"open\",\"high\",\"low\",\"close\",\"preclose\",\"volume\",\"amount\",\"adjustflag\",\"turn\",\"pctChg\"]\nfor col in numeric_columns:\n    # \u66ff\u6362\u53ef\u80fd\u5b58\u5728\u7684\u975e\u6570\u5b57\u5b57\u7b26\n    df&#91;col] = pd.to_numeric(df&#91;col], errors='coerce')\n\n# \u5904\u7406\u53ef\u80fd\u7684\u7f3a\u5931\u503c\n# 1. \u67e5\u770b\u7f3a\u5931\u503c\u60c5\u51b5\nprint(\"\u7f3a\u5931\u503c\u7edf\u8ba1\uff1a\")\nprint(df.isnull().sum())\n\n# 2. \u586b\u5145\u7f3a\u5931\u503c\uff08\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u586b\u5145\u65b9\u5f0f\uff09\n# \u5bf9\u4e8e\u4ef7\u683c\u7c7b\u6570\u636e\uff0c\u4f7f\u7528\u524d\u4e00\u5929\u7684\u503c\u586b\u5145\nprice_columns = &#91;\"open\",\"high\",\"low\",\"close\",\"preclose\"]\ndf&#91;price_columns] = df&#91;price_columns].fillna(method='ffill')\n\n# \u5bf9\u4e8e\u4ea4\u6613\u91cf\u3001\u6210\u4ea4\u989d\u7b49\uff0c\u4f7f\u75280\u586b\u5145\nvolume_columns = &#91;\"volume\",\"amount\"]\ndf&#91;volume_columns] = df&#91;volume_columns].fillna(0)\n\n# \u5bf9\u4e8e\u5176\u4ed6\u6570\u503c\u5217\uff0c\u4f7f\u7528\u5747\u503c\u586b\u5145\nother_numeric = list(set(numeric_columns) - set(price_columns) - set(volume_columns))\nfor col in other_numeric:\n    if not df&#91;col].empty and df&#91;col].notna().any():\n        df&#91;col] = df&#91;col].fillna(df&#91;col].mean())\n\n# \u8f6c\u6362\u65e5\u671f\u683c\u5f0f\ndf&#91;'date'] = pd.to_datetime(df&#91;'date'])\n\n# \u8f93\u51fa\u6570\u636e\u57fa\u672c\u4fe1\u606f\nprint(\"\\n\u6570\u636e\u57fa\u672c\u4fe1\u606f\uff1a\")\nprint(df.info())\nprint(\"\\n\u6570\u636e\u524d5\u884c\uff1a\")\nprint(df.head())\nprint(\"\\n\u6570\u636e\u540e5\u884c\uff1a\")\nprint(df.tail())\n\n# \u4fdd\u5b58\u6570\u636e\u4e3aCSV\u6587\u4ef6\uff08\u9002\u5408SPSS Modeler\u5bfc\u5165\uff09\noutput_file = 'stock_data_numeric.csv'\ndf.to_csv(output_file, index=False, encoding='utf-8')\nprint(f\"\\n\u6570\u636e\u5df2\u4fdd\u5b58\u81f3\uff1a{output_file}\")\n\n# \u767b\u51fa\u7cfb\u7edf\nbs.logout()\n\n# \u63d0\u793a\u7528\u6237\u5982\u4f55\u5728SPSS Modeler\u4e2d\u4f7f\u7528\u8be5\u6570\u636e\nprint(\"\\n\u63d0\u793a\uff1a\")\nprint(\"1. \u5df2\u5c06\u6240\u6709\u6570\u503c\u5217\u8f6c\u6362\u4e3a\u6570\u5b57\u683c\u5f0f\uff0c\u9002\u5408SPSS Modeler\u5206\u6790\")\nprint(\"2. \u65e5\u671f\u5217\u5df2\u8f6c\u6362\u4e3a\u6807\u51c6\u65e5\u671f\u683c\u5f0f\")\nprint(\"3. \u7f3a\u5931\u503c\u5df2\u8fdb\u884c\u5904\u7406\")\nprint(\"4. \u60a8\u53ef\u4ee5\u5728SPSS Modeler\u4e2d\u901a\u8fc7'CSV\u6570\u636e\u6e90'\u8282\u70b9\u5bfc\u5165\u6b64\u6587\u4ef6\u8fdb\u884c\u5206\u6790\")\nprint(\"5. \u5982\u9700\u83b7\u53d6\u5176\u4ed6\u80a1\u7968\u6216\u65f6\u95f4\u6bb5\u6570\u636e\uff0c\u8bf7\u4fee\u6539\u4ee3\u7801\u4e2d\u7684start_date\u3001end_date\u548cstock_code\u53c2\u6570\")\n<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u662f\u4f7f\u7528baostock\u83b7\u53d6\u7684\u80a1\u7968\u6570\u636e\u4e2d\u5404\u4e2a\u5b57\u6bb5\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>\u6570\u636e\u5904\u7406\u8bf4\u660e \u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1a\u4ee3\u7801\u4e2d\u5df2\u5c06\u6240\u6709\u6570\u503c\u5b57\u6bb5\uff08open\u3001high\u3001low\u3001close\u3001preclose\u3001volume\u3001amount\u3001adjustflag\u3001turn\u3001pctChg\uff09\u901a\u8fc7pd.to_numeric()\u8f6c\u6362\u4e3a\u6570\u5b57\u683c\u5f0f<\/p>\n\n\n\n<p>\u7f3a\u5931\u503c\u5904\u7406\uff1a<\/p>\n\n\n\n<p>\u4ef7\u683c\u7c7b\u6570\u636e\uff08open\u3001high\u3001low\u3001close\u3001preclose\uff09\u4f7f\u7528\u524d\u4e00\u5929\u7684\u503c\u586b\u5145 \u4ea4\u6613\u91cf\u3001\u6210\u4ea4\u989d\u7b49\uff08volume\u3001amount\uff09\u4f7f\u75280\u586b\u5145 \u5176\u4ed6\u6570\u503c\u5217\u4f7f\u7528\u5747\u503c\u586b\u5145 \u65e5\u671f\u683c\u5f0f\uff1adate\u5b57\u6bb5\u5df2\u901a\u8fc7pd.to_datetime()\u8f6c\u6362\u4e3a\u6807\u51c6\u65e5\u671f\u683c\u5f0f<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u5904\u7406\u786e\u4fdd\u4e86\u6570\u636e\u9002\u5408\u5728SPSS Modeler\u4e2d\u8fdb\u884c\u5404\u79cd\u7edf\u8ba1\u5206\u6790\uff0c\u5982\u76f8\u5173\u6027\u5206\u6790\u3001\u56de\u5f52\u5206\u6790\u7b49\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u67e5\u770b\u5f53\u524d\u7684 stocks_baostock.py \u6587\u4ef6\u5185\u5bb9\uff0c\u4e86\u89e3\u5176\u4e2d\u5305\u542b\u7684\u6570\u636e\u5b57\u6bb5\uff0c\u7136\u540e\u4e3a\u8fd9\u4e9b\u5b57\u6bb5\u63d0\u4f9b\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n\n\n\n<p>toolName: view_files<br>status: success<\/p>\n\n\n\n<p>filePath: c:\\Users\\czliu\\Documents\\python\\stocks_baostock.py<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4f7f\u7528baostock\u83b7\u53d6\u7684\u80a1\u7968\u6570\u636e\u4e2d\u5404\u4e2a\u5b57\u6bb5\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-%E5%9F%BA%E6%9C%AC%E5%AD%97%E6%AE%B5\">1. \u57fa\u672c\u5b57\u6bb5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u4e2d\u6587\u540d\u79f0<\/th><th>\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>date<\/td><td>\u65e5\u671f<\/td><td>\u65e5\u671f\u7c7b\u578b<\/td><td>\u4ea4\u6613\u65e5\u671f\uff0c\u683c\u5f0f\u4e3aYYYY-MM-DD<\/td><\/tr><tr><td>code<\/td><td>\u80a1\u7968\u4ee3\u7801<\/td><td>\u5b57\u7b26\u4e32<\/td><td>\u5982&#8221;sh.600938&#8243;\uff0c\u5176\u4e2dsh\u8868\u793a\u4e0a\u6d77\u8bc1\u5238\u4ea4\u6613\u6240<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-%E4%BB%B7%E6%A0%BC%E7%9B%B8%E5%85%B3%E5%AD%97%E6%AE%B5\">2. \u4ef7\u683c\u76f8\u5173\u5b57\u6bb5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u4e2d\u6587\u540d\u79f0<\/th><th>\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>open<\/td><td>\u5f00\u76d8\u4ef7<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u5f00\u76d8\u4ef7\u683c<\/td><\/tr><tr><td>high<\/td><td>\u6700\u9ad8\u4ef7<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u4ea4\u6613\u7684\u6700\u9ad8\u4ef7\u683c<\/td><\/tr><tr><td>low<\/td><td>\u6700\u4f4e\u4ef7<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u4ea4\u6613\u7684\u6700\u4f4e\u4ef7\u683c<\/td><\/tr><tr><td>close<\/td><td>\u6536\u76d8\u4ef7<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u6536\u76d8\u4ef7\u683c<\/td><\/tr><tr><td>preclose<\/td><td>\u524d\u6536\u76d8\u4ef7<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u524d\u4e00\u4e2a\u4ea4\u6613\u65e5\u7684\u6536\u76d8\u4ef7\u683c<\/td><\/tr><tr><td>pctChg<\/td><td>\u6da8\u8dcc\u5e45<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u6da8\u8dcc\u5e45\u767e\u5206\u6bd4\uff0c\u8ba1\u7b97\u516c\u5f0f\uff1a(\u6536\u76d8\u4ef7-\u524d\u6536\u76d8\u4ef7)\/\u524d\u6536\u76d8\u4ef7*100%<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-%E4%BA%A4%E6%98%93%E9%87%8F%E7%9B%B8%E5%85%B3%E5%AD%97%E6%AE%B5\">3. \u4ea4\u6613\u91cf\u76f8\u5173\u5b57\u6bb5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u4e2d\u6587\u540d\u79f0<\/th><th>\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>volume<\/td><td>\u6210\u4ea4\u91cf<\/td><td>\u6574\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u6210\u4ea4\u7684\u6570\u91cf\uff0c\u5355\u4f4d\u4e3a\u80a1<\/td><\/tr><tr><td>amount<\/td><td>\u6210\u4ea4\u989d<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u80a1\u7968\u6210\u4ea4\u7684\u91d1\u989d\uff0c\u5355\u4f4d\u4e3a\u5143<\/td><\/tr><tr><td>turn<\/td><td>\u6362\u624b\u7387<\/td><td>\u6d6e\u70b9\u6570<\/td><td>\u5f53\u65e5\u6362\u624b\u7387\u767e\u5206\u6bd4\uff0c\u8ba1\u7b97\u516c\u5f0f\uff1a(\u6210\u4ea4\u91cf\/\u6d41\u901a\u80a1\u6570)*100%<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-%E7%8A%B6%E6%80%81%E7%9B%B8%E5%85%B3%E5%AD%97%E6%AE%B5\">4. \u72b6\u6001\u76f8\u5173\u5b57\u6bb5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5\u540d<\/th><th>\u4e2d\u6587\u540d\u79f0<\/th><th>\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>adjustflag<\/td><td>\u590d\u6743\u72b6\u6001<\/td><td>\u6574\u6570<\/td><td>\u8868\u793a\u6570\u636e\u7684\u590d\u6743\u7c7b\u578b\uff0c\u4ee3\u7801\u4e2d\u7684adjustflag=&#8221;3&#8243;\u8868\u793a&#8221;\u524d\u590d\u6743&#8221;<\/td><\/tr><tr><td>tradestatus<\/td><td>\u4ea4\u6613\u72b6\u6001<\/td><td>\u6574\u6570<\/td><td>\u8868\u793a\u80a1\u7968\u7684\u4ea4\u6613\u72b6\u6001\uff0c1\u8868\u793a\u6b63\u5e38\u4ea4\u6613\uff0c0\u8868\u793a\u505c\u724c<\/td><\/tr><tr><td>isST<\/td><td>\u662f\u5426ST<\/td><td>\u5b57\u7b26\u4e32<\/td><td>\u8868\u793a\u80a1\u7968\u662f\u5426\u4e3aST\u80a1\u7968\uff0c1\u8868\u793a\u662fST\u80a1\u7968\uff0c0\u8868\u793a\u975eST\u80a1\u7968<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E8%AF%B4%E6%98%8E\">\u6570\u636e\u5904\u7406\u8bf4\u660e<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u4ee3\u7801\u4e2d\u5df2\u5c06\u6240\u6709\u6570\u503c\u5b57\u6bb5\uff08open\u3001high\u3001low\u3001close\u3001preclose\u3001volume\u3001amount\u3001adjustflag\u3001turn\u3001pctChg\uff09\u901a\u8fc7<code>pd.to_numeric()<\/code>\u8f6c\u6362\u4e3a\u6570\u5b57\u683c\u5f0f<\/li>\n\n\n\n<li><strong>\u7f3a\u5931\u503c\u5904\u7406<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4ef7\u683c\u7c7b\u6570\u636e\uff08open\u3001high\u3001low\u3001close\u3001preclose\uff09\u4f7f\u7528\u524d\u4e00\u5929\u7684\u503c\u586b\u5145<\/li>\n\n\n\n<li>\u4ea4\u6613\u91cf\u3001\u6210\u4ea4\u989d\u7b49\uff08volume\u3001amount\uff09\u4f7f\u75280\u586b\u5145<\/li>\n\n\n\n<li>\u5176\u4ed6\u6570\u503c\u5217\u4f7f\u7528\u5747\u503c\u586b\u5145<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u65e5\u671f\u683c\u5f0f<\/strong>\uff1adate\u5b57\u6bb5\u5df2\u901a\u8fc7<code>pd.to_datetime()<\/code>\u8f6c\u6362\u4e3a\u6807\u51c6\u65e5\u671f\u683c\u5f0f<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e9b\u5904\u7406\u786e\u4fdd\u4e86\u6570\u636e\u9002\u5408\u5728SPSS Modeler\u4e2d\u8fdb\u884c\u5404\u79cd\u7edf\u8ba1\u5206\u6790\uff0c\u5982\u76f8\u5173\u6027\u5206\u6790\u3001\u56de\u5f52\u5206\u6790\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4f7f\u7528baostock\u83b7\u53d6\u7684\u80a1\u7968\u6570\u636e\u4e2d\u5404\u4e2a\u5b57\u6bb5\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a \u6570\u636e\u5904\u7406\u8bf4\u660e \u6570 <span class=\"readmore\"><a href=\"http:\/\/xc.ipyingshe.net:5347\/?p=6325\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-6325","post","type-post","status-publish","format-standard","hentry","category-2","category-24"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6325"}],"version-history":[{"count":2,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6325\/revisions"}],"predecessor-version":[{"id":6454,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6325\/revisions\/6454"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6325"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}