Tuesday, February 3, 2026
HomeLanguagesPython | Pandas.to_datetime()

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:

  1. arg: An integer, string, float, list or dict object to convert in to Date time object.
  2. dayfirst: Boolean value, places day first if True.
  3. yearfirst: Boolean value, places year first if True.
  4. utc: Boolean value, Returns time in UTC if True.
  5. format: String input to tell position of day, month and year.

Return type:

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.

Python


# 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
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
124 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11979 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6935 POSTS0 COMMENTS
Umr Jansen
6919 POSTS0 COMMENTS