Horje
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.

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




Reffered: https://www.geeksforgeeks.org


AI ML DS

Related
How To Save Multiple Numpy Arrays How To Save Multiple Numpy Arrays
Kuki Chatbot vs. Mitsuku: Which AI Chatbot Is More Human-Like? Kuki Chatbot vs. Mitsuku: Which AI Chatbot Is More Human-Like?
Convert Numpy Array To Xarray Convert Numpy Array To Xarray
Top 12 AI Tools for Financial Analysts: 2024 Edition Top 12 AI Tools for Financial Analysts: 2024 Edition
What is Data Interpolation? What is Data Interpolation?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13