{"id":4768,"date":"2022-02-23T23:16:16","date_gmt":"2022-02-23T14:16:16","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=4768"},"modified":"2022-02-23T23:18:01","modified_gmt":"2022-02-23T14:18:01","slug":"%e3%80%90python%e3%80%91pandas%e3%81%a7%e6%95%b0%e5%80%a4%e5%80%a4%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%97%a5%e6%99%82%e5%9e%8b%e3%81%b8%e5%a4%89%e6%8f%9b%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=4768","title":{"rendered":"\u3010Python\u3011pandas\u3067\u6570\u5024\u5024\u30c7\u30fc\u30bf\u3092\u65e5\u6642\u578b\u3078\u5909\u63db\u3059\u308b\u65b9\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=4768\/#%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=4768\/#%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%83%87%E3%83%BC%E3%82%BF\" >\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf<\/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=4768\/#%E6%96%87%E5%AD%97%E5%88%97%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\" >\u6587\u5b57\u5217\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<\/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=4768\/#%E6%95%B0%E5%80%A4int%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\" >\u6570\u5024(int)\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<\/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=4768\/#float%E5%9E%8B%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\" >float\u578b\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<\/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=4768\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><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\u306b\u306fto_datetime\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u6587\u5b57\u5217\u578b\u3092\u65e5\u6642\u578b\u306b\u4e00\u767a\u3067\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3057\u304b\u3057\u3001\u6570\u5024\u2192\u65e5\u6642\u578b\u3078\u306e\u5909\u63db\u306b\u3064\u3044\u3066\u306f\u6570\u5024\u306e\u578b\u306b\u3082\u6c17\u3092\u914d\u3089\u306a\u3044\u3068\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u305f\u3081\u3001\u5bfe\u51e6\u6cd5\u306b\u3064\u3044\u3066\u30e1\u30e2\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%83%87%E3%83%BC%E3%82%BF\"><\/span>\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3068\u3057\u3066\u3001\u6587\u5b57\u5217\u3001\u6570\u5024(int)\u3001\u6570\u5024(float)\u306e\u4e09\u3064\u306e\u578b\u306e\u5217\u3092\u6301\u3064\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import pandas as pd\ndf = pd.DataFrame([[&#39;20210101&#39;,20210101,20210101.0],[&#39;20210102&#39;,20210102,20210102.0]], columns=[&#39;col1&#39;,&#39;col2&#39;,&#39;col3&#39;])<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"86\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/02\/image-3.png\" alt=\"\" class=\"wp-image-4769\"\/><\/figure>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.dtypes<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">col1     object\ncol2      int64\ncol3    float64\ndtype: object<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%96%87%E5%AD%97%E5%88%97%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\"><\/span>\u6587\u5b57\u5217\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5358\u306bto_datetime\u3092\u4f7f\u7528\u3059\u308b\u3060\u3051\u3067\u5909\u63db\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>pd.to_datetime(df[&#39;col1&#39;])<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">0   2021-01-01\n1   2021-01-02\nName: col1, dtype: datetime64[ns]<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%95%B0%E5%80%A4int%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\"><\/span>\u6570\u5024(int)\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>int\u578b\u304b\u3089\u65e5\u6642\u578b\u3078\u306f\u76f4\u63a5\u5909\u63db\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u304a\u304b\u3057\u306a\u5024\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>NG\u4f8b<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>pd.to_datetime(df[&#39;col2&#39;])<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">0   1970-01-01 00:00:00.020210101\n1   1970-01-01 00:00:00.020210102\nName: col2, dtype: datetime64[ns]<\/pre>\n\n\n\n<p>int\u578b\u306e\u5834\u5408\u306f\u4e00\u65e6astype()\u3067\u6587\u5b57\u5217\u578b\u306b\u5909\u63db\u3057\u305f\u5f8c\u306bto_datetime\u3092\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u5909\u63db\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>OK\u4f8b<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>pd.to_datetime(df[&#39;col2&#39;].astype(str))<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">0   2021-01-01\n1   2021-01-02\nName: col2, dtype: datetime64[ns]<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"float%E5%9E%8B%E3%80%80%E2%86%92%E3%80%80%E6%97%A5%E6%99%82%E5%9E%8B%E3%81%B8%E3%81%AE%E5%A4%89%E6%8F%9B\"><\/span>float\u578b\u3000\u2192\u3000\u65e5\u6642\u578b\u3078\u306e\u5909\u63db<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>float\u578b\u306e\u5834\u5408\u306f\u3055\u3089\u306b\u3082\u3046\u3072\u3068\u624b\u9593\u5fc5\u8981\u3067\u3059\u3002\u4f8b\u3048\u3070float\u578b\u306e\u3082\u306e\u3092\u540c\u3058\u3088\u3046\u306bastype\u3067\u6587\u5b57\u5217\u578b\u306b\u5909\u63db\u3057\u3066to_datetime\u3092\u9069\u7528\u3059\u308b\u3068ParserError\u3092\u5410\u3044\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>NG\u4f8b<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>pd.to_datetime(df[&#39;col3&#39;].astype(str))<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong>ParserError<\/strong>: year 20210101 is out of range: 20210101.0<\/pre>\n\n\n\n<p>\u305d\u306e\u305f\u3081\u3001float\u578b\u306e\u5834\u5408\u306fint\u578b\u306b\u5909\u63db\u3057\u305f\u4e0a\u3067\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3001to_datetime\u3092\u9069\u7528\u3059\u308c\u3070\u5909\u63db\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>pd.to_datetime(df[&#39;col3&#39;].astype(int).astype(str))<\/code><\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\">0   2021-01-01\n1   2021-01-02\nName: col3, dtype: datetime64[ns]<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><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>\u6570\u5024\u578b\u306e\u5909\u63db\u306e\u969b\u306f\u307e\u305a\u306fint\u578b\u306b\u76f4\u3059\u3068\u3044\u3046\u3053\u3068\u3092\u5fd8\u308c\u305a\u306b\u3002\u5c0f\u6570\u70b9\u4ee5\u4e0b\u306e\u6570\u5b57\u304c\u3042\u308b\u5834\u5408\u306a\u3069\u306fround\u306a\u3069\u3067\u4e38\u3081\u3066\u304b\u3089\u4f7f\u7528\u3059\u308b\u3079\u304d\u5834\u5408\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Python\u306epandas\u306b\u306fto_datetime\u3068\u3044\u3046\u30e1\u30bd&#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=4768","footnotes":""},"categories":[7],"tags":[93],"class_list":["post-4768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-pandas","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4768","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=4768"}],"version-history":[{"count":3,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4768\/revisions"}],"predecessor-version":[{"id":4773,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4768\/revisions\/4773"}],"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=4768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}