Skip to content
Related Articles

Related Articles

Get Seconds from timestamp in Python-Pandas

Improve Article
Save Article
  • Last Updated : 05 Apr, 2022
Improve Article
Save Article

In this program our task is to create a program in python which will extract seconds from a given timestamp. We will use the s attribute of the DateTime object to extract the second.
Example : If we have a data frame that contains 5 different time stamps such as: 
 

Timestamp
2012-12-11 04:12:01
2013-10-13 04:12:04
2014-12-14 04:12:05
2015-11-15 04:12:06
2016-10-15 04:12:07

And our task is to extract that seconds from given timestamps. So the output here will be: 
 

seconds
1
4
5
6
7

Example 1 : 
 

Python3




# importing the module
import pandas as pd
 
# generating 10 timestamp starting from '2016-10-10 09:21:12'
date1 = pd.Series(pd.date_range('2016-10-10 09:21:12',
                                periods = 10, freq = 's'))
 
# converting pandas series into data frame
df = pd.DataFrame(dict(GENERATEDTIMESTAMP = date1))
 
# extracting seconds from time stamp
df['extracted_seconds_timestamp'] = df['GENERATEDTIMESTAMP'].dt.second
 
# displaying  DataFrame
display(df)

Output : 
 

Example 2 : 
 

Python3




# importing the module
import pandas as pd
 
# generating 5 timestamp starting from '2020-01-01 00:00:00'
date1 = pd.Series(pd.date_range('2020-01-01 00:00:00',
                                periods = 5, freq = 's'))
 
# converting pandas series into data frame
df = pd.DataFrame(dict(GENERATEDTIMESTAMP = date1))
 
# extracting seconds from time stamp
df['extracted_seconds_timestamp'] = df['GENERATEDTIMESTAMP'].dt.second
 
# displaying  DataFrame
display(df)

Output : 
 

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!