Reduce precision pandas timestamp dataframe

Learn, how to reduce precision pandas timestamp dataframe in Python? By Pranit Sharma Last updated : October 06, 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.

Problem statement

Suppose we are given the data frame with the column of timestamp with timestamp column containing date and time values and the time value is too much précised. We need to find a way to reduce this precision.

Reducing precision pandas timestamp dataframe

For this purpose, the astype() method where will pass the 'datetime64' argument. This method casts a pandas object to a specified dtype dtype.

The datetime is a library in Python which is a collection of date and time. Inside Datetime, we can access date and time in any format, but usually date is present in the format of "yy-mm-dd" and time is present in the format of "HH:MM:SS"’.


  • yy means year
  • mm means month
  • dd means day
  • HH means hours
  • MM means minutes
  • SS means seconds

Let us understand with the help of an example,

Python program to reduce precision pandas timestamp dataframe

# Importing pandas
import pandas as pd

# Creating a dictionary
d = {'time':[
    '2011-01-10 07:19:19.647342',
    '2015-09-10 07:19:19.647342',
    '2010-03-10 03:15:19.647342',
    '2010-03-10 08:29:19.647342']

# Creating a dataframe
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Converting date column into datetime
df['time'] = pd.to_datetime(df['time'])

# Change dtype
df['time'] = df['time'].astype('datetime64[s]')

# Display df
print("New df:\n",df,"\n")


The output of the above program is:

Example: Reduce precision pandas timestamp dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.