How to Extract Nanoseconds from DateTime in Pandas Series
To extract nanoseconds from DateTime in the Pandas Series we use the dt.nanosecond attribute of the Pandas library.
Let’s understand it better with an example.
Example:
Use the Series.dt.nanosecond attribute to return the nanosecond of the DateTime in 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( '2008-2-9 08:20:21' , periods = 5 , freq = '9N' )) # Creating the index idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] # set the index sr.index = idx result = sr.dt.nanosecond # print the result print (result) |
Output :
As we can see in the output, the dt.nanosecond attribute has successfully accessed and returned the nanosecond of the DateTime in the underlying data of the given series object.
Pandas Series dt.nanosecond | Get Nanoseconds From DateTime Series
Pandas dt.nanosecond attribute returns a NumPy array containing the nanosecond of the DateTime in the underlying data of the given series object.
Example
Python3
import pandas as pd sr = pd.Series(pd.date_range( '2012-12-12 12:12' , periods = 5 , freq = '5N' )) idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] sr.index = idx result = sr.dt.nanosecond print (result) |
Output