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
Python Pandas - Return the microseconds from Timedelta object
To return the microseconds from a Timedelta object, use the timedelta.microseconds property. This property extracts only the microseconds component from the timedelta, not including days, hours, minutes, or seconds.
Syntax
timedelta.microseconds
Creating a Timedelta Object
First, let's create a Timedelta object with various time components ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('7 days 20 min 15 s 35 ms')
print("Timedelta:")
print(timedelta)
Timedelta: 7 days 00:20:15.035000
Extracting Microseconds
Now let's extract the microseconds component using the microseconds property ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('7 days 20 min 15 s 35 ms')
# Extract the microseconds value
microseconds_value = timedelta.microseconds
print("Timedelta:")
print(timedelta)
print("\nMicroseconds component:")
print(microseconds_value)
Timedelta: 7 days 00:20:15.035000 Microseconds component: 35000
Understanding the Result
The result shows 35000 microseconds, which corresponds to the 35 milliseconds in our original timedelta. Since 1 millisecond = 1000 microseconds, 35 ms = 35,000 microseconds.
Multiple Examples
Let's see how the microseconds property works with different time values ?
import pandas as pd
# Different timedelta examples
examples = [
pd.Timedelta('500 microseconds'),
pd.Timedelta('2.5 seconds'),
pd.Timedelta('1 minute 750 milliseconds'),
pd.Timedelta('3 hours 45 minutes 12.345 seconds')
]
for i, td in enumerate(examples, 1):
print(f"Example {i}: {td}")
print(f"Microseconds: {td.microseconds}")
print()
Example 1: 0 days 00:00:00.000500 Microseconds: 500 Example 2: 0 days 00:00:02.500000 Microseconds: 500000 Example 3: 0 days 00:01:00.750000 Microseconds: 750000 Example 4: 0 days 03:45:12.345000 Microseconds: 345000
Key Points
- The
microsecondsproperty returns only the microseconds component (0-999999) - It does not include days, hours, minutes, or full seconds
- Milliseconds are converted to microseconds (1 ms = 1000 ?s)
- The maximum value is 999,999 microseconds
Conclusion
The microseconds property provides easy access to the microseconds component of a Timedelta object. This is useful when you need precise time measurements or when working with sub-second timing operations.
