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 - Get the seconds from Timedelta object using integer input
To return the seconds from a Timedelta object, use the timedelta.seconds property. The seconds property extracts the seconds component from a Timedelta object when given integer input.
Creating a Timedelta Object
First, let's create a Timedelta object using integer input with unit 's' for seconds ?
import pandas as pd
# Create a Timedelta object with 50 seconds
timedelta = pd.Timedelta(50, unit='s')
# Display the Timedelta
print("Timedelta...")
print(timedelta)
Timedelta... 0 days 00:00:50
Extracting Seconds Value
Use the .seconds property to extract the seconds component from the Timedelta object ?
import pandas as pd
# Create a Timedelta object with 50 seconds
timedelta = pd.Timedelta(50, unit='s')
# Display the Timedelta
print("Timedelta...")
print(timedelta)
# Extract the seconds value
seconds_value = timedelta.seconds
# Display the seconds
print("\nTimedelta (seconds value)...")
print(seconds_value)
Timedelta... 0 days 00:00:50 Timedelta (seconds value)... 50
Working with Larger Values
When dealing with larger time values, the .seconds property only returns the seconds portion within a day ?
import pandas as pd
# Create Timedelta objects with different values
td1 = pd.Timedelta(3725, unit='s') # 1 hour, 2 minutes, 5 seconds
td2 = pd.Timedelta(90000, unit='s') # More than 1 day
print("Timedelta 1:", td1)
print("Seconds from td1:", td1.seconds)
print()
print("Timedelta 2:", td2)
print("Seconds from td2:", td2.seconds)
Timedelta 1: 0 days 01:02:05 Seconds from td1: 3725 Timedelta 2: 1 days 01:00:00 Seconds from td2: 3600
Key Points
| Property | Description | Range |
|---|---|---|
.seconds |
Total seconds within the current day | 0 to 86399 |
.total_seconds() |
Total seconds across all days | No limit |
Conclusion
The .seconds property extracts the seconds component from a Timedelta object created with integer input. Use unit='s' to specify seconds when creating the Timedelta object.
Advertisements
