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 seconds from Timedelta object
To return the seconds from a Timedelta object, use the timedelta.seconds property. This property extracts only the seconds component from a Timedelta object.
Syntax
timedelta.seconds
Creating a Timedelta Object
First, let's create a Timedelta object with various time components ?
import pandas as pd
# Create a Timedelta object with seconds, milliseconds, and nanoseconds
timedelta = pd.Timedelta('10 s 15 ms 33 ns')
print("Timedelta...")
print(timedelta)
Timedelta... 0 days 00:00:10.015000033
Extracting Seconds
Use the seconds property to get only the seconds component ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('10 s 15 ms 33 ns')
# Extract the seconds value
seconds_value = timedelta.seconds
print("Timedelta:", timedelta)
print("Seconds component:", seconds_value)
Timedelta: 0 days 00:00:10.015000033 Seconds component: 10
Multiple Examples
Here are more examples showing different Timedelta objects ?
import pandas as pd
# Different Timedelta objects
td1 = pd.Timedelta('2 hours 30 minutes 45 seconds')
td2 = pd.Timedelta('1 day 5 seconds')
td3 = pd.Timedelta(seconds=25)
print("Timedelta 1:", td1)
print("Seconds:", td1.seconds)
print()
print("Timedelta 2:", td2)
print("Seconds:", td2.seconds)
print()
print("Timedelta 3:", td3)
print("Seconds:", td3.seconds)
Timedelta 1: 0 days 02:30:45 Seconds: 9045 Timedelta 2: 1 days 00:00:05 Seconds: 5 Timedelta 3: 0 days 00:00:25 Seconds: 25
Key Points
- The
secondsproperty returns only the seconds component within a day (0-86399) - For hours and minutes, they are converted to seconds in the output
- Days are not included in the seconds count
- Milliseconds and nanoseconds are not included
Conclusion
The timedelta.seconds property extracts the seconds component from a Pandas Timedelta object. It returns the total seconds within a day, including converted hours and minutes, but excludes days and sub-second components.
Advertisements
