Open In App

Convert Datetime Object To Seconds, Minutes & Hours Using pandas

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We can convert a datetime object to seconds, minutes, and hours using pandas in Python by accessing the attributes of the datetime object and performing the necessary calculations.

Convert Datetime Object To Seconds, Minutes & Hours Using pandas

Convert a datetime Object to Seconds

To retrieve the seconds from the given string, we use the second attribute .

Python3




import pandas as pd
 
# Create a datetime object
dt = pd.to_datetime('2024-02-07 12:34:56')
 
# Extract seconds, minutes, and hours
seconds = dt.second
print("Seconds:", seconds)


Output:

Seconds: 56

Convert datetime Object to Minutes

We can use minute attribute to convert the datetime object to minutes.

Python3




import pandas as pd
 
# Create a datetime object
dt = pd.to_datetime('2024-02-07 12:34:56')
 
minutes = dt.minute
print("Minutes:", minutes)


Output:

Minutes: 34 

Convert datetime Object to Hours

We can use hour attribute to convert the datetime object to hours.

Python3




import pandas as pd
 
# Create a datetime object
dt = pd.to_datetime('2024-02-07 12:34:56')
hours = dt.hour
 
print("Hours:", hours)


Output:

Hours: 12



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads