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



Convert Datetime Object To Seconds, Minutes & Hours Using pandas

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.

Similar Reads

Convert Datetime Object To Seconds, Minutes & Hours Using pandas

Convert a datetime Object to Seconds...