Python | Pandas.to_datetime()
When a csv file is imported and a Data Frame is made, the Date time objects in the file are read as a string object rather a Date Time object and Hence it’s very tough to perform operations like Time difference on a string rather a Date Time object. Pandas to_datetime() method helps to convert string Date time into Python Date time object.
Syntax:
pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=’unix’, cache=False)
Parameters:arg: An integer, string, float, list or dict object to convert in to Date time object.
dayfirst: Boolean value, places day first if True.
yearfirst: Boolean value, places year first if True.
utc: Boolean value, Returns time in UTC if True.
format: String input to tell position of day, month and year.
Return type: Date time object series.
For link of the CSV file used, click here.
Example #1: String to Date
In the following example, a csv file is read and the date column of Data frame is converted into Date Time object from a string object.
# importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv("todatetime.csv") # overwriting data after changing format data["Date"]= pd.to_datetime(data["Date"]) # info of data data.info() # display data |
Output:
As shown in the image, the Data Type of Date column was object but after using to_datetime(), it got converted into a date time object.
Before operation-

After Operation-

Example #2: Exception while converting Time
Time object can also be converted with this method. But since in the Time column, a date isn’t specified and hence Pandas will put Today’s date automatically in that case.
# importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv("todatetime.csv") # overwriting data after changing format data["Time"]= pd.to_datetime(data["Time"]) # info of data data.info() # display data |
Output:
As shown in the output, a date (2018-07-07) that is Today’s date is already added with the Date time object.

Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- bin() in Python
- SQL using Python | Set 1
- max() and min() in Python
- Python | a += b is not always a = a + b
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


