{"id":898,"date":"2023-07-28T09:27:40","date_gmt":"2023-07-28T09:27:40","guid":{"rendered":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/"},"modified":"2023-07-28T09:27:40","modified_gmt":"2023-07-28T09:27:40","slug":"convertire-le-colonne-in-datatime-panda","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/","title":{"rendered":"Come convertire le colonne in datetime in pandas"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti essere interessato a convertire una o pi\u00f9 colonne di un DataFrame panda nel formato DateTime. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.to_datetime.html\" target=\"_blank\" rel=\"noopener noreferrer\">to_datetime()<\/a> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial mostra diversi esempi di utilizzo di questa funzione sul seguente DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> numpy <span style=\"color: #107d3f;\">as<\/span> np\n<span style=\"color: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame<\/span>\ndf = pd.DataFrame({'event': ['A', 'B', 'C'],\n                   'start_date': ['20150601', '20160201', '20170401'],\n                   'end_date': ['20150608', '20160209', '20170416'] })\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n\tevent start_date end_date\n0 A 20150601 20150608\n1 B 20160201 20160209\n2 C 20170401 201704161\n\n<span style=\"color: #008080;\">#view column data types\n<\/span>df. <span style=\"color: #3366ff;\">dtypes\n\n<span style=\"color: #000000;\">event object\nstart_date object\nend_date object\ndtype:object<\/span><\/span><\/strong><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Esempio 1: convertire una singola colonna in DateTime<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come convertire la colonna &#8220;start_date&#8221; da una stringa al formato DateTime:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert start_date to DateTime format<\/span>\ndf['start_date'] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df['start_date'])\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n        event start_date end_date\n0 A 2015-06-01 20150608\n1 B 2016-02-01 20160209\n2 C 2017-04-01 20170416\n\n<span style=\"color: #008080;\">#view column date types\n<\/span>df. <span style=\"color: #3366ff;\">dtypes<\/span>\n\nevent object\nstart_date datetime64[ns]\nend_date object\ndtype:object<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che la funzione to_datetime() \u00e8 intelligente e di solito pu\u00f2 dedurre il formato di data corretto da utilizzare, ma puoi anche specificare quale formato utilizzare con l&#8217;argomento <strong>format<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert start_date to DateTime format<\/span>\ndf['start_date'] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df['start_date'], format=' <span style=\"color: #993300;\">%Y%m%d<\/span> ')\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n        event start_date end_date\n0 A 2015-06-01 20150608\n1 B 2016-02-01 20160209\n2 C 2017-04-01 20170416\n\n<span style=\"color: #008080;\">#view column date types\n<\/span>df. <span style=\"color: #3366ff;\">dtypes<\/span>\n\nevent object\nstart_date datetime64[ns]\nend_date object\ndtype:object<\/strong><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Esempio 2: convertire pi\u00f9 colonne in DateTime<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come convertire le colonne &#8220;start_date&#8221; e &#8220;end_date&#8221; dalle stringhe ai formati DateTime:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert start_date and end_date to DateTime formats<\/span>\ndf[['start_date', 'end_date']] = df[['start_date', 'end_date']]. <span style=\"color: #3366ff;\">apply<\/span> (pd. <span style=\"color: #3366ff;\">to_datetime<\/span> )\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n\tevent start_date end_date\n0 A 2015-06-01 2015-06-08\n1 B 2016-02-01 2016-02-09\n2 C 2017-04-01 2017-04-16\n\n<span style=\"color: #008080;\">#view column date types\n<\/span>df. <span style=\"color: #3366ff;\">dtypes<\/span>\n\nevent object\nstart_date datetime64[ns]\nend_date datetime64[ns]\ndtype:object<\/strong><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Esempio 3: convertire le colonne nel formato DateTime con secondi<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">In alcuni casi, potresti anche avere colonne che includono una data oltre a ore, minuti e secondi, come il seguente DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#createDataFrame<\/span>\ndf = pd.DataFrame({'event': ['A', 'B', 'C'],\n                   'start_date': ['20150601043000', '20160201054500', '20170401021215'],\n                   'end_date': ['20150608', '20160209', '20170416'] })\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n        event start_date end_date\n0 A 20150601043000 20150608\n1 B 20160201054500 20160209\n2 C 20170401021215 20170416\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ancora una volta, la funzione to_datetime() \u00e8 intelligente e di solito pu\u00f2 dedurre il formato corretto da utilizzare senza che lo specifichiamo:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert start_date to DateTime format<\/span>\ndf['start_date'] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df['start_date'])\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n        event start_date end_date\n0 A 2015-06-01 04:30:00 20150608\n1 B 2016-02-01 05:45:00 20160209\n2 C 2017-04-01 02:12:15 20170416\n\n<span style=\"color: #008080;\">#view column date types\n<\/span>df. <span style=\"color: #3366ff;\">dtypes<\/span>\n\nevent object\nstart_date datetime64[ns]\nend_date object\ndtype:object<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Naturalmente, in natura probabilmente incontrerai una variet\u00e0 di strani formati DateTime, quindi potresti dover utilizzare l&#8217;argomento <strong>format<\/strong> per dire a Python esattamente quale formato DateTime utilizzare.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In questi casi, fai riferimento a <a href=\"https:\/\/strftime.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">questa pagina<\/a> per un elenco completo<\/span> <span style=\"color: #000000;\">degli operatori %DateTime che puoi utilizzare per specificare i formati.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/it\/convertire-datetime-in-data-panda\/\">Come convertire DateTime fino ad oggi in Panda<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/convertire-una-stringa-in-panda-fluttuanti\/\">Come convertire le stringhe in float in Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti essere interessato a convertire una o pi\u00f9 colonne di un DataFrame panda nel formato DateTime. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione to_datetime() . Questo tutorial mostra diversi esempi di utilizzo di questa funzione sul seguente DataFrame: import numpy as np import pandas as pd #createDataFrame df = pd.DataFrame({&#8216;event&#8217;: [&#8216;A&#8217;, &#8216;B&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come convertire le colonne in DateTime in Pandas \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come convertire le colonne in DateTime in Pandas \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T09:27:40+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/\",\"url\":\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/\",\"name\":\"Come convertire le colonne in DateTime in Pandas \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T09:27:40+00:00\",\"dateModified\":\"2023-07-28T09:27:40+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come convertire le colonne in datetime in pandas\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come convertire le colonne in DateTime in Pandas \u2013 Statorials","description":"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/","og_locale":"it_IT","og_type":"article","og_title":"Come convertire le colonne in DateTime in Pandas \u2013 Statorials","og_description":"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.","og_url":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/","og_site_name":"Statorials","article_published_time":"2023-07-28T09:27:40+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/","url":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/","name":"Come convertire le colonne in DateTime in Pandas \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T09:27:40+00:00","dateModified":"2023-07-28T09:27:40+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione su come convertire le colonne DataFrame nei formati DateTime nei panda.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/convertire-le-colonne-in-datatime-panda\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come convertire le colonne in datetime in pandas"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/898"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}