{"id":4743,"date":"2022-01-31T23:24:22","date_gmt":"2022-01-31T14:24:22","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=4743"},"modified":"2022-01-31T23:24:22","modified_gmt":"2022-01-31T14:24:22","slug":"%e3%80%90python%e3%80%91pandas-%e3%81%aeresample%e3%81%a7typeerror-only-valid-with-datetimeindex-timedeltaindex-or-periodindex-but-got-an-instance-of-rangeindex%e3%81%8c%e5%87%ba%e3%81%9f","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=4743","title":{"rendered":"\u3010Python\u3011pandas \u306eresample\u3067&#8221;TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of &#8216;RangeIndex'&#8221;\u304c\u51fa\u305f\u6642\u306e\u5bfe\u51e6\u6cd5"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#%E6%A6%82%E8%A6%81\" >\u6982\u8981<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#%E6%A4%9C%E8%A8%BC%E7%92%B0%E5%A2%83\" >\u691c\u8a3c\u74b0\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#resample%E3%81%AE%E4%BD%BF%E3%81%84%E3%81%A9%E3%81%93%E3%82%8D%E3%81%AE%E4%BE%8B\" >resample\u306e\u4f7f\u3044\u3069\u3053\u308d\u306e\u4f8b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E4%BE%8B\" >\u30a8\u30e9\u30fc\u306e\u4f8b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E5%8E%9F%E5%9B%A0\" >\u30a8\u30e9\u30fc\u306e\u539f\u56e0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/obenkyolab.com\/?p=4743\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"\u6982\u8981\"><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>python\u306epandas\u3067\u65e5\u4ed8\u306a\u3069\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u96c6\u8a08\u3057\u305f\u5834\u5408\u306b\u4f7f\u7528\u3067\u304d\u308bresample\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u307e\u3059\u3002\u9031\u5358\u4f4d\u3001\u65e5\u5358\u4f4d\u3001\u6642\u9593\u5358\u4f4d\u306a\u3069\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u96c6\u8a08\u3059\u308b\u5834\u5408\u306b\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u304c\u3001resample\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u306e\u306b\u9069\u3057\u305f\u5f62\u306b\u3057\u306a\u3044\u3068Type\u30a8\u30e9\u30fc\u3092\u5410\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306fType\u30a8\u30e9\u30fc\u306e\u4f8b\u3068\u3001\u767a\u751f\u3057\u305f\u5834\u5408\u306e\u5bfe\u51e6\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u691c\u8a3c\u74b0\u5883\"><span class=\"ez-toc-section\" id=\"%E6%A4%9C%E8%A8%BC%E7%92%B0%E5%A2%83\"><\/span>\u691c\u8a3c\u74b0\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>python 3.8.10<\/li><li>pandas 1.3.5<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"resample\u306e\u4f7f\u3044\u3069\u3053\u308d\u306e\u4f8b\"><span class=\"ez-toc-section\" id=\"resample%E3%81%AE%E4%BD%BF%E3%81%84%E3%81%A9%E3%81%93%E3%82%8D%E3%81%AE%E4%BE%8B\"><\/span>resample\u306e\u4f7f\u3044\u3069\u3053\u308d\u306e\u4f8b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4f8b\u3048\u3070\u6b21\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u304c\u3042\u3063\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.head()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full waku\"><img loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"142\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/01\/image-7.png\" alt=\"\" class=\"wp-image-4744\"\/><\/figure>\n\n\n\n<p>\u3053\u308c\u3092\u6708\u6bce\u306e\u96c6\u8a08\u3057\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7d50\u679c\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u3044\u3046\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3093\u306a\u3068\u304d\u306fpandas\u306eresample\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.resample(&#39;M&#39;).sum()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full waku\"><img loading=\"lazy\" decoding=\"async\" width=\"147\" height=\"117\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/01\/image-8.png\" alt=\"\" class=\"wp-image-4745\"\/><\/figure>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u5143\u30c7\u30fc\u30bf\u304b\u3089\u540c\u3058\u6708\u3092\u30b0\u30eb\u30fc\u30d4\u30f3\u30b0\u3057\u3001\u30b0\u30eb\u30fc\u30d7\u5185\u3067value\u306e\u5217\u306e\u5024\u3092\u8db3\u3057\u5408\u308f\u305b\u305f\u7d50\u679c\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305f\u3060\u3057\u3001\u3053\u306eresample\u95a2\u6570\u3067\u3059\u304c\u3001\u7c21\u5358\u306b\u898b\u3048\u3066\u3061\u3087\u3063\u3068\u7656\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u304b\u3089\u3088\u304f\u8d77\u3053\u308b\u30a8\u30e9\u30fc\u306e\u4f8b\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u30a8\u30e9\u30fc\u306e\u4f8b\"><span class=\"ez-toc-section\" id=\"%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E4%BE%8B\"><\/span>\u30a8\u30e9\u30fc\u306e\u4f8b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u30a8\u30e9\u30fc\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"resample_error1.py\" data-lang=\"Python\"><code>import pandas as pd\ndf = pd.DataFrame([[&#39;2021\/01\/01 00:00:00&#39;, 1], [&#39;2021\/01\/10 00:00:00&#39;, 2], [&#39;2021\/02\/01 00:00:00&#39;, 3], [&#39;2021\/02\/10 00:00:00&#39;, 4]], columns=[&#39;datetime&#39;, &#39;value&#39;] )\ndf.resample(&#39;M&#39;).sum()<\/code><\/pre><\/div>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of &#8216;RangeIndex&#8217;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u30a8\u30e9\u30fc\u306e\u539f\u56e0\"><span class=\"ez-toc-section\" id=\"%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E5%8E%9F%E5%9B%A0\"><\/span>\u30a8\u30e9\u30fc\u306e\u539f\u56e0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3067\u3042\u308c\u3070resample\u304c\u3059\u3050\u4f7f\u3048\u308b\u308f\u3051\u3067\u306f\u306a\u304f\u3001resample\u578b\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b\u306f\u5927\u524d\u63d0\u3068\u3057\u3066datetime\u578b\u306eSeries\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u305f\u3081\u3001datetime\u578b\u306e\u5217\u3092\u7528\u610f\u3057\u3066\u3084\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"resample_error2.py\" data-lang=\"Python\"><code>import pandas as pd\ndf = pd.DataFrame([[&#39;2021\/01\/01 00:00:00&#39;, 1], [&#39;2021\/01\/10 00:00:00&#39;, 2], [&#39;2021\/02\/01 00:00:00&#39;, 3], [&#39;2021\/02\/10 00:00:00&#39;, 4]], columns=[&#39;datetime&#39;, &#39;value&#39;] )\ndf[&#39;datetime&#39;] = pd.to_datetime(df[&#39;datetime&#39;])\ndf.resample(&#39;M&#39;).sum()<\/code><\/pre><\/div>\n\n\n\n<p>pandas\u306eto_datetime\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001&#8217;datetime&#8217;\u5217\u3092datetime\u578b\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u3067\u3082\u307e\u3060\u540c\u69d8\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u306fresample\u306e\u3082\u3046\u4e00\u3064\u306e\u4f7f\u7528\u6761\u4ef6\u3068\u3057\u3066\u3001\u3053\u306edatetime\u578b\u306e\u5217\u306fIndex\u3068\u3057\u3066\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6b21\u306e\u30b3\u30fc\u30c9\u3067\u3042\u308c\u3070\u30a8\u30e9\u30fc\u306f\u767a\u751f\u3057\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"resample_without_error.py\" data-lang=\"Python\"><code>import pandas as pd\ndf = pd.DataFrame([[&#39;2021\/01\/01 00:00:00&#39;, 1], [&#39;2021\/01\/10 00:00:00&#39;, 2], [&#39;2021\/02\/01 00:00:00&#39;, 3], [&#39;2021\/02\/10 00:00:00&#39;, 4]], columns=[&#39;datetime&#39;, &#39;value&#39;] )\ndf[&#39;datetime&#39;] = pd.to_datetime(df[&#39;datetime&#39;]) # \u65e5\u6642\u60c5\u5831\u304c\u5165\u3063\u305f\u5217\u3092datetime\u578b\u3078\u5909\u63db\ndf.set_index(&#39;datetime&#39;, inplace=True) #index\u306bdatetime\u5217\u3092\u6307\u5b9a\u3057\u3001\u7f6e\u304d\u63db\u3048\ndf.resample(&#39;M&#39;).sum()<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u307e\u3068\u3081\"><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>pandas resample\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306b\u306fdatetime\u578b\u306b\u3059\u308b\u3053\u3068\u3068\u3001index\u306b\u305d\u306e\u5217\u3092\u6307\u5b9a\u3057\u3066\u304a\u304f\u3053\u3068\u304c\u5fc5\u8981\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 python\u306epandas\u3067\u65e5\u4ed8\u306a\u3069\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u96c6\u8a08\u3057\u305f\u5834&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=4743","footnotes":""},"categories":[7],"tags":[],"class_list":["post-4743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4743"}],"version-history":[{"count":1,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4743\/revisions"}],"predecessor-version":[{"id":4746,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4743\/revisions\/4746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}