Open In App

Pandas Series dt.time | Extract Time from Time Stamp in Series

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

The Series.dt.time attribute returns a NumPy array containing time values of the timestamps in a Pandas series.

Example

Python3




import pandas as pd
sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30',
                '2010-4-22 09:25', '2019-11-8 02:22'])
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
sr.index = idx
sr = pd.to_datetime(sr)
result = sr.dt.time
print(result)


Output:

Syntax

Syntax: Series.dt.time 

Parameter : None 

Returns : NumPy array containing time values

How to Extract Time Value From DateTime Series

To extract the time value from the DateTime Pandas Series we use the dt.time attribute of the Pandas library. 

Let us understand better with an example:

Example: 

Use the Series.dt.time attribute to return the time property of the underlying data of the given Series object.

Python3




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-12 12:12',
                       periods = 5, freq = 'H'))
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)


Output

datetime series created

Now we will use Series.dt.time attribute to return the time property of the underlying data of the given Series object.

Python3




# return the time
result = sr.dt.time
  
# print the result
print(result)


Output :

Extracted hour part from timestamp

As we can see in the output, the Series.dt.time attribute has successfully accessed and returned the time property of the underlying data in the given series object.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads