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 than a Date Time object Hence it’s very tough to perform operations like Time difference on a string rather than a Date Time object. Pandas to_datetime() method helps to convert string Date time into Python Date time object.
Pandas.to_datetime() Syntax
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: Datetime
Pandas.to_datetime() in Pandas Example
Pandas to_datetime() is used to convert different data types into datetime objects. We will see different examples on how to use it:
Convert a Pandas String to Datetime
To convert date and time data saved as texts into datetime objects, use Pandas.to_datetime(). The format consists of the date and time.
Python3
import pandas as pd
d_string = "2023-09-17 14:30:00"
dt_obj = pd.to_datetime(d_string)
print (dt_obj)
|
Output:
2023-09-17 14:30:00
Convert Pandas Numerical values to Datetime
The datetime objects can be created from numerical numbers that represent time, such as seconds since the Unix epoch. We can specify the unit of the input data by using the unit argument.
Python3
import pandas as pd
unix_timestamp = 1721700500
dt_obj = pd.to_datetime(unix_timestamp, unit = 's' )
print (dt_obj)
|
Output:
2024-07-23 02:08:20
Convert Pandas Column to DateTime
This will explain how to work with date and time data using the Pandas library. The main objective is to transform date and time information from a CSV file into a format that makes analysis easier to understand and more useful.
For the link to the CSV file used, click here.
Example 1: Converting Date Format using Pandas
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.
Python3
import pandas as pd
data = pd.read_csv( '/content/todatetime.csv' )
data[ 'Date' ] = pd.to_datetime(data[ 'Date' ])
data.info()
print (data.head())
|
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 1000 non-null datetime64[ns]
1 Time 1000 non-null object
dtypes: datetime64[ns](1), object(1)
memory usage: 15.8+ KB
Date Time
0 1993-08-06 12:42 PM
1 1996-03-31 6:53 AM
2 1993-04-23 11:17 AM
3 2005-03-04 1:00 PM
4 1998-01-24 4:47 PM
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.
Example 2: Converting Time Format using Pandas
Exception while converting 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.
Python3
import pandas as pd
data = pd.read_csv( '/content/todatetime.csv' )
data[ 'Time' ] = pd.to_datetime(data[ 'Time' ])
data.info()
print (data.head())
|
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 1000 non-null object
1 Time 1000 non-null datetime64[ns]
dtypes: datetime64[ns](1), object(1)
memory usage: 15.8+ KB
Date Time
0 8/6/1993 2023-10-12 12:42:00
1 3/31/1996 2023-10-12 06:53:00
2 4/23/1993 2023-10-12 11:17:00
3 3/4/2005 2023-10-12 13:00:00
4 1/24/1998 2023-10-12 16:47:00
As shown in the output, a date (2018-07-07) that is Today’s date is already added with the Date time object.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Dec, 2023
Like Article
Save Article