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 integer input
To return the microseconds from a Timedelta object, use the timedelta.microseconds property. This property extracts only the microseconds component from the timedelta object.
Syntax
timedelta_object.microseconds
Creating a Timedelta with Microseconds
First, import the required library and create a Timedelta object using integer input with unit 'us' for microseconds −
import pandas as pd
# Create a Timedelta object with 55 microseconds
timedelta = pd.Timedelta(55, unit='us')
print("Timedelta...")
print(timedelta)
Timedelta... 0 days 00:00:00.000055
Extracting Microseconds
Use the microseconds property to get the microseconds component −
import pandas as pd
# Create a Timedelta object with 55 microseconds
timedelta = pd.Timedelta(55, unit='us')
print("Timedelta...")
print(timedelta)
# Extract microseconds value
microseconds_value = timedelta.microseconds
print("\nMicroseconds value:")
print(microseconds_value)
Timedelta... 0 days 00:00:00.000055 Microseconds value: 55
Example with Larger Values
Here's an example with a larger microsecond value to demonstrate the extraction −
import pandas as pd
# Create a Timedelta with 123456 microseconds
timedelta = pd.Timedelta(123456, unit='us')
print("Timedelta:")
print(timedelta)
print("\nMicroseconds component:")
print(timedelta.microseconds)
Timedelta: 0 days 00:00:00.123456 Microseconds component: 123456
Key Points
- The
microsecondsproperty returns only the microseconds component (0-999999) - Use unit='us' when creating Timedelta objects with microsecond values
- The property returns an integer representing the microseconds
Conclusion
The microseconds property provides a simple way to extract the microseconds component from Pandas Timedelta objects. This is useful when you need to work with specific time components in your data analysis.
Advertisements
