Python Pandas - Return the nanoseconds from Timedelta object

To extract the nanoseconds component from a Pandas Timedelta object, use the nanoseconds property. This property returns only the nanoseconds portion (0-999) of the timedelta, not the total nanoseconds.

Creating a Timedelta Object

First, import pandas and create a Timedelta object with various time components ?

import pandas as pd

# Create a Timedelta object with days, minutes, seconds, milliseconds, and nanoseconds
timedelta = pd.Timedelta('4 days 10 min 25 s 15 ms 33 ns')
print("Timedelta:", timedelta)
Timedelta: 4 days 00:10:25.015000033

Extracting Nanoseconds Component

Use the nanoseconds property to get only the nanoseconds portion ?

import pandas as pd

timedelta = pd.Timedelta('4 days 10 min 25 s 15 ms 33 ns')
nanoseconds_value = timedelta.nanoseconds

print("Original Timedelta:", timedelta)
print("Nanoseconds component:", nanoseconds_value)
Original Timedelta: 4 days 00:10:25.015000033
Nanoseconds component: 33

Understanding the Difference

The nanoseconds property returns only the nanoseconds component (0-999), while other methods return different values ?

import pandas as pd

timedelta = pd.Timedelta('2 s 500 ms 750 us 250 ns')

print("Timedelta:", timedelta)
print("Nanoseconds component:", timedelta.nanoseconds)
print("Total nanoseconds:", timedelta.total_seconds() * 1e9)
print("Value (internal representation):", timedelta.value)
Timedelta: 0 days 00:00:02.500750250
Nanoseconds component: 250
Total nanoseconds: 2500750250.0
Value (internal representation): 2500750250

Key Points

  • nanoseconds property returns only the nanoseconds component (0-999)
  • For total nanoseconds, use total_seconds() * 1e9 or value property
  • The nanoseconds component is the remainder after accounting for larger time units

Conclusion

The nanoseconds property extracts only the nanoseconds component from a Timedelta object. Use total_seconds() * 1e9 if you need the total duration in nanoseconds instead.

Updated on: 2026-03-26T16:03:27+05:30

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements