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 microseconds from Timedelta object using string input
To return the microseconds from a Timedelta object, use the timedelta.microseconds property. This property extracts only the microseconds component from the timedelta.
Creating a Timedelta with Microseconds
First, import pandas and create a Timedelta object using string input with microseconds ?
import pandas as pd
# Create a Timedelta object with microseconds
timedelta = pd.Timedelta('12 min 40 us')
# Display the Timedelta
print("Timedelta...")
print(timedelta)
Timedelta... 0 days 00:12:00.000040
Extracting Microseconds
Use the microseconds property to get only the microseconds component ?
import pandas as pd
# Create a Timedelta object with microseconds
timedelta = pd.Timedelta('12 min 40 us')
# Return the microseconds value
microseconds_value = timedelta.microseconds
print("Timedelta:", timedelta)
print("Microseconds component:", microseconds_value)
Timedelta: 0 days 00:12:00.000040 Microseconds component: 40
Multiple Microseconds Examples
Here are examples with different microsecond values ?
import pandas as pd
# Create different Timedelta objects with microseconds
timedeltas = [
pd.Timedelta('5 seconds 100 us'),
pd.Timedelta('1 hour 500 us'),
pd.Timedelta('2 days 750 us')
]
for td in timedeltas:
print(f"Timedelta: {td}")
print(f"Microseconds: {td.microseconds}")
print()
Timedelta: 0 days 00:00:05.000100 Microseconds: 100 Timedelta: 0 days 01:00:00.000500 Microseconds: 500 Timedelta: 2 days 00:00:00.000750 Microseconds: 750
Key Points
| Property | Description | Range |
|---|---|---|
microseconds |
Microseconds component only | 0-999999 |
total_seconds() |
Total duration in seconds | Variable |
Conclusion
The microseconds property extracts only the microseconds component from a Timedelta object. Use string input with 'us' unit to specify microseconds when creating Timedelta objects.
Advertisements
