Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Return the nanoseconds from Timedelta object using integer input
To return the nanoseconds from a Timedelta object, use the timedelta.nanoseconds property. This property extracts only the nanoseconds component from the total time duration.
Syntax
timedelta.nanoseconds
Creating a Timedelta with Nanoseconds
First, import the required library and create a Timedelta object using integer input with unit 'ns' ?
import pandas as pd
# Create a Timedelta object with 35 nanoseconds
timedelta = pd.Timedelta(35, unit='ns')
print("Timedelta:", timedelta)
Timedelta: 0 days 00:00:00.000000035
Extracting Nanoseconds Value
Use the nanoseconds property to get the nanoseconds component ?
import pandas as pd
timedelta = pd.Timedelta(35, unit='ns')
nanoseconds_value = timedelta.nanoseconds
print("Original Timedelta:", timedelta)
print("Nanoseconds component:", nanoseconds_value)
Original Timedelta: 0 days 00:00:00.000000035 Nanoseconds component: 35
Example with Larger Values
Here's an example with a larger nanoseconds value to see how it's displayed ?
import pandas as pd
# Create Timedelta with 1250 nanoseconds
timedelta = pd.Timedelta(1250, unit='ns')
print("Timedelta:", timedelta)
print("Nanoseconds component:", timedelta.nanoseconds)
print("Total nanoseconds:", timedelta.total_seconds() * 1e9)
Timedelta: 0 days 00:00:00.000001250 Nanoseconds component: 250 Total nanoseconds: 1250.0
Key Points
- The
nanosecondsproperty returns only the nanoseconds component (0-999) - For total nanoseconds, use
total_seconds() * 1e9 - Timedelta displays time in days, hours, minutes, seconds, and fractional seconds format
Conclusion
The nanoseconds property extracts the nanoseconds component from a Timedelta object. Use pd.Timedelta(value, unit='ns') to create nanosecond-based time deltas and access the component with the nanoseconds attribute.
Advertisements
