{"id":964975,"date":"2024-12-27T04:33:30","date_gmt":"2024-12-26T20:33:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964975.html"},"modified":"2024-12-27T04:33:32","modified_gmt":"2024-12-26T20:33:32","slug":"python%e5%a6%82%e4%bd%95%e6%8c%89%e6%9c%88%e4%bb%bd%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/964975.html","title":{"rendered":"python\u5982\u4f55\u6309\u6708\u4efd\u904d\u5386"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181516\/016d7d76-6517-4bf0-b641-145a1d83d90c.webp\" alt=\"python\u5982\u4f55\u6309\u6708\u4efd\u904d\u5386\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6309\u6708\u4efd\u904d\u5386\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u65e5\u671f\u65f6\u95f4\u5e93\u6216pandas\u5e93\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u4e00\u4e2a\u5e74\u4efd\u4e2d\u7684\u6240\u6709\u6708\u4efd\u3001\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u3001\u4ee5\u53ca\u5206\u6790\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408Python\u7684datetime\u6a21\u5757\u6765\u904d\u5386\u6708\u4efd\u3002\u4f8b\u5982\uff0c\u5229\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u6bcf\u4e2a\u6708\u7684\u65e5\u671f\u5bf9\u8c61\uff0c\u5e76\u8fdb\u884c\u8fed\u4ee3\u3002\u5176\u6b21\uff0cPython\u4e2d\u7684<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7<code>date_range<\/code>\u51fd\u6570\u751f\u6210\u6309\u6708\u4efd\u9891\u7387\u7684\u65e5\u671f\u5e8f\u5217\u3002\u7b2c\u4e09\uff0c\u4f7f\u7528\u7eafPython\u7684\u65b9\u5f0f\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\u6570\u636e\u7ed3\u6784\uff0c\u7ed3\u5408\u5faa\u73af\u6765\u5b9e\u73b0\u6708\u4efd\u904d\u5386\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u8f83\u76f4\u89c2\u4e14\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u6708\u4efd\u904d\u5386\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757\u904d\u5386\u6708\u4efd<\/h3>\n<\/p>\n<p><p>Python\u7684<code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u57fa\u672c\u529f\u80fd\u3002\u901a\u8fc7\u8be5\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u4e00\u4e2a\u5e74\u4efd\u4e2d\u7684\u6bcf\u4e2a\u6708\u4efd\uff0c\u5e76\u6267\u884c\u904d\u5386\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528for\u5faa\u73af\u548cdatetime\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u5e76\u9010\u6708\u589e\u52a0\u6708\u4efd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6708\u4efd\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<p>def iterate_months(year):<\/p>\n<p>    date = datetime(year, 1, 1)<\/p>\n<p>    while date.year == year:<\/p>\n<p>        print(date.strftime(&quot;%Y-%m&quot;))<\/p>\n<p>        if date.month == 12:<\/p>\n<p>            break<\/p>\n<p>        date = date.replace(month=date.month + 1)<\/p>\n<p>iterate_months(2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4ece\u7ed9\u5b9a\u5e74\u4efd\u76841\u67081\u65e5\u5f00\u59cb\uff0c\u9010\u6708\u589e\u52a0\u6708\u4efd\u76f4\u523012\u6708\uff0c\u901a\u8fc7<code>strftime<\/code>\u51fd\u6570\u683c\u5f0f\u5316\u8f93\u51fa\u6bcf\u4e2a\u6708\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528relativedelta\u589e\u52a0\u6708\u4efd<\/h4>\n<\/p>\n<p><p><code>dateutil<\/code>\u5e93\u4e2d\u7684<code>relativedelta<\/code>\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u65e5\u671f\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>from dateutil.relativedelta import relativedelta<\/p>\n<p>def iterate_months_with_relativedelta(year):<\/p>\n<p>    date = datetime(year, 1, 1)<\/p>\n<p>    while date.year == year:<\/p>\n<p>        print(date.strftime(&quot;%Y-%m&quot;))<\/p>\n<p>        date += relativedelta(months=1)<\/p>\n<p>iterate_months_with_relativedelta(2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>relativedelta<\/code>\u5141\u8bb8\u6211\u4eec\u8f7b\u677e\u5730\u589e\u52a0\u6216\u51cf\u5c11\u65e5\u671f\u7684\u7279\u5b9a\u90e8\u5206\uff08\u5982\u6708\u4efd\uff09\uff0c\u5728\u5904\u7406\u65e5\u671f\u504f\u79fb\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93\u904d\u5386\u6708\u4efd<\/h3>\n<\/p>\n<p><p>Python\u7684<code>pandas<\/code>\u5e93\u662f\u6570\u636e\u5206\u6790\u7684\u5229\u5668\uff0c\u5c24\u5176\u64c5\u957f\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u901a\u8fc7<code>pandas<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u6309\u6708\u4efd\u9891\u7387\u7684\u65f6\u95f4\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528pandas\u7684date_range\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>date_range<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u9891\u7387\u7684\u65e5\u671f\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def iterate_months_with_pandas(year):<\/p>\n<p>    start_date = f&quot;{year}-01-01&quot;<\/p>\n<p>    end_date = f&quot;{year}-12-31&quot;<\/p>\n<p>    dates = pd.date_range(start=start_date, end=end_date, freq=&#39;MS&#39;)<\/p>\n<p>    for date in dates:<\/p>\n<p>        print(date.strftime(&quot;%Y-%m&quot;))<\/p>\n<p>iterate_months_with_pandas(2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>freq=&#39;MS&#39;<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u65e5\u671f\u9891\u7387\u4e3a\u6bcf\u6708\u7684\u5f00\u59cb\uff0c\u4ece\u800c\u751f\u6210\u6bcf\u4e2a\u6708\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7eafPython\u5b9e\u73b0\u6708\u4efd\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u65e5\u671f\u65f6\u95f4\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7eafPython\u7684\u65b9\u5f0f\u5b9e\u73b0\u6708\u4efd\u904d\u5386\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u7b80\u5355\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u5217\u8868\u548c\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4e00\u4e2a\u5305\u542b\u6708\u4efd\u540d\u79f0\u6216\u6570\u5b57\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u6708\u4efd\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def iterate_months_with_list():<\/p>\n<p>    months = [f&quot;{i:02}&quot; for i in range(1, 13)]<\/p>\n<p>    for month in months:<\/p>\n<p>        print(f&quot;2023-{month}&quot;)<\/p>\n<p>iterate_months_with_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6708\u4efd\u6570\u5b57\u7684\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u8f93\u51fa\u6bcf\u4e2a\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u6309\u6708\u4efd\u904d\u5386\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u6570\u636e\u5206\u6790\u5e08\u53ef\u80fd\u9700\u8981\u6309\u6708\u4efd\u6c47\u603b\u9500\u552e\u6570\u636e\u3001\u8ba1\u7b97\u6bcf\u6708\u7684\u5e73\u5747\u6e29\u5ea6\u7b49\u3002\u5728\u8fd9\u4e9b\u573a\u666f\u4e0b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\uff1a<\/strong> \u4f7f\u7528<code>pandas<\/code>\u5e93\u662f\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u671f\u64cd\u4f5c\uff1a<\/strong> \u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u65e5\u671f\u8ba1\u7b97\u548c\u64cd\u4f5c\uff0c<code>dateutil<\/code>\u7684<code>relativedelta<\/code>\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u5355\u4efb\u52a1\uff1a<\/strong> \u5bf9\u4e8e\u7b80\u5355\u7684\u6708\u4efd\u904d\u5386\u4efb\u52a1\uff0c\u4f7f\u7528\u7eafPython\u7684\u5faa\u73af\u548c\u5217\u8868\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u6309\u6708\u4efd\u904d\u5386\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u65e5\u671f\u904d\u5386\u4efb\u52a1\uff0c\u8fd8\u662f\u8fdb\u884c\u590d\u6742\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u6307\u5b9a\u5e74\u4efd\u7684\u6bcf\u4e2a\u6708\u4efd\u5217\u8868\uff1f<\/strong><br \/>\u8981\u751f\u6210\u6307\u5b9a\u5e74\u4efd\u7684\u6bcf\u4e2a\u6708\u4efd\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>calendar<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u4e00\u5e74\u4e2d\u6bcf\u4e2a\u6708\u4efd\u7684\u540d\u79f0\uff1a  <\/p>\n<pre><code class=\"language-python\">import calendar\n\nyear = 2023\nmonths = [calendar.month_name[i] for i in range(1, 13)]\nprint(months)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6708\u4efd\u540d\u79f0\u7684\u5217\u8868\uff0c\u5982\u201cJanuary, February, \u2026, December\u201d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6309\u6708\u4efd\u8fdb\u884c\u6570\u636e\u5206\u7ec4\uff1f<\/strong><br \/>\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6309\u6708\u4efd\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u901a\u8fc7<code>pd.Grouper<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;date&#39;: [&#39;2023-01-15&#39;, &#39;2023-01-20&#39;, &#39;2023-02-18&#39;, &#39;2023-03-10&#39;],\n        &#39;value&#39;: [10, 20, 30, 40]}\ndf = pd.DataFrame(data)\ndf[&#39;date&#39;] = pd.to_datetime(df[&#39;date&#39;])\nmonthly_groups = df.groupby(pd.Grouper(key=&#39;date&#39;, freq=&#39;M&#39;)).sum()\nprint(monthly_groups)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u6309\u6708\u4efd\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6708\u7684\u603b\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u6309\u6708\u4efd\u7684\u65e5\u671f\u8303\u56f4\uff1f<\/strong><br \/>\u82e5\u60f3\u521b\u5efa\u4e00\u4e2a\u6309\u6708\u4efd\u7684\u65e5\u671f\u8303\u56f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>date_range<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u751f\u6210\u4ece2023\u5e741\u6708\u52302023\u5e7412\u6708\u7684\u6bcf\u4e2a\u6708\u7684\u7b2c\u4e00\u5929\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndate_range = pd.date_range(start=&#39;2023-01-01&#39;, end=&#39;2023-12-31&#39;, freq=&#39;MS&#39;)\nprint(date_range)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u80fd\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u6708\u7b2c\u4e00\u5929\u7684\u65e5\u671f\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6309\u6708\u4efd\u904d\u5386\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u65e5\u671f\u65f6\u95f4\u5e93\u6216pandas\u5e93\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef [&hellip;]","protected":false},"author":3,"featured_media":964979,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964975"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=964975"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964975\/revisions"}],"predecessor-version":[{"id":964980,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964975\/revisions\/964980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964979"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}