Using pandas to_datetime with timestamps
Last Updated :
24 Nov, 2022
In this article, we are going to convert timestamps to datetime using the to_datetime() method from the pandas package. A timestamp is encoded information or a set of characters identifying when a certain event occurred, giving date and time of day, which can be accurate to a small fraction of a second.
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.
Example 1: Timestamps with to_datetime.
Here we are converting the CSV file into a dataframe using pandas.DataFrame() method after reading the contents of the file using pandas.read_csv(), the timestamps column from the data Dataframe is given as an argument in the to_datetime() for it to be converted into DateTime. unit='s' is used to convert the values of the timestamp column to epoch time after converting the values to DateTime it is stored in a column called 'Datetime' in the Dataframe.
File Used:

Code:
Python3
# import packages
import pandas as pd
# creating a dataframe from the csv file
data = pd.DataFrame(pd.read_csv('timestamps.csv'))
# viewing our dataframe
print("Original dataframe")
display(data)
# unit='s' to convert it into epoch time
data['Datetime'] = pd.to_datetime(data['timestamps'],
unit='s')
# checking our dataframe once again
print("Timestamps")
display(data)
Output:

Example 2: Formatting the Datetime column.
The code is just the same as the previous example, the add-on is formatting the 'DateTime' column. The Datetime column in the previous example can be further modified using strftime() which takes a string as an argument. strftime() returns an Index of formatted strings specified by date_format, which supports the same string format as the python standard library.
Syntax: strftime(format)
Code:
Python3
# import packages
import pandas as pd
import datetime
# creating a dataframe from the csv file
data = pd.DataFrame(pd.read_csv('timestamps.csv'))
# unit='s' to convert it into epoch time
data['Datetime'] = pd.to_datetime(data['timestamps'],
unit='s')
data['Modified Datetime'] = data['Datetime'].dt.strftime('%d-%m-%Y %H:%M')
# checking our dataframe once again
display(data)
Output:

Example 3: Using unit='ms' in the to_datetime() method.
The pd.to_datetime() method with a timestamp as argument and unit='ms', calculating the number of milliseconds to the Unix epoch start.
Python3
# import packages
import pandas as pd
# unit='ms' to calculate the number
# of milliseconds
date = pd.to_datetime(1550767605,
unit = 'ms')
# checking our dataframe once again
print(date)
Output:
1970-01-18 22:46:07.605000
Similar Reads
Pandas Timestamp To Datetime A timestamp is a representation of a specific point in time, expressed as a combination of date and time information. In data processing, timestamps are used to note the occurrence of events, record the time at which data was generated or modified, and provide a chronological ordering of data.Pandas
3 min read
Python | Pandas Timestamp.to_datetime64 Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.to_datetime64() function return a numpy.datetime64 object with ânsâ p
2 min read
Python | Pandas Timestamp.to_datetime64 Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.to_datetime64() function return a numpy.datetime64 object with ânsâ p
2 min read
Pandas.to_datetime()-Python pandas.to_datetime() converts argument(s) to datetime. This function is essential for working with date and time data, especially when parsing strings or timestamps into Python's datetime64 format used in Pandas. For Example:Pythonimport pandas as pd d = ['2025-06-21', '2025-06-22'] res = pd.to_date
3 min read
Pandas.to_datetime()-Python pandas.to_datetime() converts argument(s) to datetime. This function is essential for working with date and time data, especially when parsing strings or timestamps into Python's datetime64 format used in Pandas. For Example:Pythonimport pandas as pd d = ['2025-06-21', '2025-06-22'] res = pd.to_date
3 min read
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read