Pandas converting row with UNIX timestamp (in milliseconds) to datetime

Given a pandas dataframe, we have to convert row with UNIX timestamp (in milliseconds) to datetime. By Pranit Sharma Last updated : September 29, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Epoch Time or UNIX Time

Epoch time or UNIX time is the standard convention for representing real-world time. The epoch time or the real-world time starts at 00:00:00 UTC where UTC stands for Coordinated Universal Time.

Converting row with UNIX timestamp to datetime

For this purpose, we are going to create a DataFrame with some specific columns of object type, we will use pandas.to_datetime() method inside which we will pass the unit of time as ms.

Syntax:

pandas.to_datetime(
    arg, 
    errors='raise', 
    dayfirst=False, 
    yearfirst=False, 
    utc=None, 
    format=None, 
    exact=True, 
    unit=None, 
    infer_datetime_format=False, 
    origin='unix', 
    cache=True
    )

Let us understand with the help of an example,

Python program to convert row with UNIX timestamp (in milliseconds) to datetime

# Importing pandas package
import pandas as pd

# Importing methods from sklearn
from sklearn.preprocessing import MinMaxScaler

# Creating a dictionary
d = {'Time':['2016-12-11 15:06:03.321','2017-10-09 16:07:04.365','2018-09-08 17:08:05.366']}

# Creating DataFrame
df = pd.DataFrame(d)

# Display the DataFrame
print("Original DataFrame:\n",df,"\n")
print("Data Type of this column is:-->> ",df['Time'].dtype)

# Creating unic time in new column
df['Time'] = pd.to_datetime(df['Time'])

# Display result
print("Result:\n",df,"\n")
print("The new Data Type of this column is:-->> ",df['Time'].dtype)

Output

The output of the above program is:

Converting row with UNIX timestamp to datetime - Output

Reference:

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.