{"id":1275,"date":"2024-01-03T21:00:00","date_gmt":"2024-01-03T21:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1275"},"modified":"2024-01-04T08:53:11","modified_gmt":"2024-01-04T08:53:11","slug":"pandas-datetime-to-date","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-datetime-to-date\/","title":{"rendered":"Convert Datetime to Date Column in Pandas (with code)"},"content":{"rendered":"\n<p>One of the common tasks that you might encounter while working with Pandas is converting DateTime to a date string. This requirement often arises when you are only interested in the date part of the DateTime object, excluding the time information. In this article, we will learn about some easy ways to convert a DateTime object to a date string in Pandas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Convert Datetime to Date in Pandas?<\/strong><\/h2>\n\n\n\n<p>Before learning about methods, let&#8217;s revise the concept of DateTime. It is a set of dates and times combined into one unit. It follows the format of &#8220;yyyy-mm-dd HH:MM:SS&#8221;, where &#8220;yyyy-mm-dd&#8221; represents the date, and &#8220;HH:MM:SS&#8221; represents the time.<\/p>\n\n\n\n<p>DateTime objects can be used in a lot of places in our projects. We can also query the specific Year, Month, Day, Hour, Minute, or even Second from the object. However, many times we are not able to directly use the DateTime object.&nbsp;<\/p>\n\n\n\n<p>Let us first see an example of creating a DateTime object:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">from datetime import datetime\n\n# Using the now() method to get current time and date\ncurrent_time = datetime.now()\nprint(current_time)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>2023-12-28 03:59:30.858756<\/code><\/pre>\n\n\n\n<p>The datetime.now() function returns a DateTime object representing the current date and time. You can then access various components of the date and time using attributes like year, month, day, hour, minute, second, and microsecond.<\/p>\n\n\n\n<p>When you are grouping data based on date values, converting datetime to date allows you to focus on the date component alone. This is particularly useful when summarizing or analyzing data on a daily, monthly, or yearly basis. Also, when merging dataframes on date columns, conversion of datetime to date can facilitate matching records with the same date, even if they have different time components.<\/p>\n\n\n\n<p>Now, let us discuss the various methods we can use to convert the DateTime to date.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the .date() Function<\/strong><\/h3>\n\n\n\n<p><strong><strong> The date() function<\/strong> is an in-built function that is used to convert the DateTime object into a date string. It is a simple and direct way to extract the date from a DateTime object. <\/strong><\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nfrom datetime import datetime\n\n# Create a custom sample dataset\ndata = {'Timestamp': [datetime(2023, 1, 15, 8, 30),\n                      datetime(2023, 1, 16, 12, 45),\n                      datetime(2023, 1, 17, 10, 0),\n                      datetime(2023, 1, 18, 14, 15)]}\n\n# Create a DataFrame from the sample dataset\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint(&quot;Original DataFrame:&quot;)\nprint(df)\n\n# Convert Datetime object to date string using .date()\ndf['Date'] = df['Timestamp'].dt.date\n\n# Display the DataFrame with the new 'Date' column\nprint(&quot;\\nDataFrame with Date String:&quot;)\nprint(df[['Date']])<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n            Timestamp\n0 2023-01-15 08:30:00\n1 2023-01-16 12:45:00\n2 2023-01-17 10:00:00\n3 2023-01-18 14:15:00\n\nDataFrame with Date String:\n         Date\n0  2023-01-15\n1  2023-01-16\n2  2023-01-17\n3  2023-01-18\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the normalize() Method<\/strong><\/h3>\n\n\n\n<p>Another built-in Python function that we can use for this conversion is the .normalize() method. It is particularly useful when you need to normalize the data by extracting the date from DateTime. Let&#8217;s see how it works:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nfrom datetime import datetime\n\n# Create a custom sample dataset\ndata = {'Timestamp': [datetime(2023, 1, 15, 8, 30),\n                      datetime(2023, 1, 16, 12, 45),\n                      datetime(2023, 1, 17, 10, 0),\n                      datetime(2023, 1, 18, 14, 15)]}\n\n# Create a DataFrame from the sample dataset\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint(&quot;Original DataFrame:&quot;)\nprint(df)\n\n# Convert Datetime object to date string using .normalize()\ndf['Date'] = df['Timestamp'].dt.normalize()\n\n# Display the DataFrame with the new 'Date' column\nprint(&quot;\\nDataFrame with Date String:&quot;)\nprint(df[['Date']])<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n            Timestamp\n0 2023-01-15 08:30:00\n1 2023-01-16 12:45:00\n2 2023-01-17 10:00:00\n3 2023-01-18 14:15:00\n\nDataFrame with Date String:\n        Date\n0 2023-01-15\n1 2023-01-16\n2 2023-01-17\n3 2023-01-18\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using a Custom Function<\/strong><\/h3>\n\n\n\n<p>Aside from the built-in functions provided by Pandas, you can also create a custom function to convert Pandas datetime to date. This can be particularly helpful when you have specific requirements that can&#8217;t be met by the built-in functions. This method provides more flexibility and control over the formatting of the date, making it a useful tool when working with complex datasets.<\/p>\n\n\n\n<p>The following example will help you better understand it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\nfrom datetime import datetime\n\n# Create a custom sample dataset\ndata = {'Timestamp': [datetime(2023, 1, 15, 8, 30),\n                      datetime(2023, 1, 16, 12, 45),\n                      datetime(2023, 1, 17, 10, 0),\n                      datetime(2023, 1, 18, 14, 15)]}\n\n# Create a DataFrame from the sample dataset\ndf = pd.DataFrame(data)\n\n# Custom Function to extract the date\ndef extract_date(dt):\n    return dt.date()\n\n# Display the original DataFrame\nprint(&quot;Original DataFrame:&quot;)\nprint(df)\n\n# Convert Datetime object to date string using custom function\ndf['Date'] = df['Timestamp'].apply(extract_date)\n\n# Display the DataFrame with the new 'Date' column\nprint(&quot;\\nDataFrame with Date String:&quot;)\nprint(df[['Date']])<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n            Timestamp\n0 2023-01-15 08:30:00\n1 2023-01-16 12:45:00\n2 2023-01-17 10:00:00\n3 2023-01-18 14:15:00\n\nDataFrame with Date String:\n         Date\n0  2023-01-15\n1  2023-01-16\n2  2023-01-17\n3  2023-01-18\n<\/code><\/pre>\n\n\n\n<p>The next thing you might have to do is to <a href=\"https:\/\/favtutor.com\/blogs\/strftime-in-python\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blogs\/strftime-in-python\">format the date and time in Python<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we learned about built-in and custom-made methods we can use to convert a DateTime to Date string in Pandas. Date strings are human-readable and provide a clear representation of the date and time information. This can be useful for logging, displaying information to users, or other situations where a human-readable format is preferred.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are some easy methods to convert datetime to date column in Pandas using date method and normalize function.<\/p>\n","protected":false},"author":10,"featured_media":1277,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37],"class_list":["post-1275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1275"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1275\/revisions"}],"predecessor-version":[{"id":1303,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1275\/revisions\/1303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1277"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}