Python Pandas Timedelta.nanoseconds Attribute

Timedelta is a subclass of datetime.timedelta, and it performs similarly. It’s Pandas’ version of Python’s datetime.timedelta. In most circumstances, it is interchangeable with it.

Pandas Timedelta.nanoseconds Attribute:

In pandas.Timedelta, the Timedelta.nanoseconds attribute gives the number of nanoseconds.

Syntax:

Timedelta.nanoseconds

Parameters: It has no arguments

Return Value:

The number of nanoseconds value is returned by the Timedelta.nanoseconds attribute.

Pandas Timedelta.nanoseconds Attribute in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random Timestamp in the format(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) to the Timedelta() function of the pandas module to get the Timedelta object.
  • Store it in a variable
  • Print the above obtained Timedelta object
  • Apply nanoseconds attribute on the above Timedelta object to get the number of nanoseconds in the above Timedelta object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random Timestamp in the format(days, hours, minutes, seconds,
# milliseconds, microseconds, nanoseconds) to the Timedelta() function of 
# the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta('7 days 10:15:08.000000122') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 

# Apply nanoseconds attribute on the above Timedelta object to get the 
# number of nanoseconds in the above Timedelta object
print("The number of nanoseconds in the above Timedelta object:")
print(timedelta_obj.nanoseconds)

Output:

The above obtained Timedelta object: 7 days 10:15:08.000000122
The number of nanoseconds in the above Timedelta object:
122

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random Timestamp in the format(days, minutes, nanoseconds) to the Timedelta() function of the pandas module to get the Timedelta object.
  • Store it in a variable
  • Print the above obtained Timedelta object
  • Apply nanoseconds attribute on the above Timedelta object to get the number of nanoseconds in the above Timedelta object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random Timestamp in the format(days, minutes, nanoseconds) to the Timedelta() 
# function of the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta('6 days 30 minutes 20ns') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 
print()
# Apply nanoseconds attribute on the above Timedelta object to get the 
# number of nanoseconds in the above Timedelta object
print("The number of nanoseconds in the above Timedelta object:")
print(timedelta_obj.nanoseconds)

Output:

The above obtained Timedelta object: 6 days 00:30:00.000000020

The number of nanoseconds in the above Timedelta object:
20

Example3

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random number and unit as argument to the Timedelta() 
# function of the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta(80, unit='ns') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 
print()
# Apply nanoseconds attribute on the above Timedelta object to get the 
# number of nanoseconds in the above Timedelta object
print("The number of nanoseconds in the above Timedelta object:")
print(timedelta_obj.nanoseconds)

Output:

The above obtained Timedelta object: 0 days 00:00:00.000000080

The number of nanoseconds in the above Timedelta object:
80