Timedelta seconds – Python Pandas Timedelta.seconds Attribute

Timedelta seconds: Timedelta is a subclass of datetime.timedelta, and it performs similarly. It’s Pandas’ version of Python’s datetime.timedelta. In most circumstances, it is interchangeable with it.

Pandas Timedelta.seconds Attribute:

In pandas.Timedelta, the Timedelta.seconds attribute gives the number of seconds.

Syntax:

Timedelta.seconds

Parameters: It has no arguments

Return Value:

The number of seconds value is returned by the Timedelta.seconds attribute.

Pandas Timedelta.seconds Attribute in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random Timestamp in the format(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) to the Timedelta() function of the pandas module to get the Timedelta object.
  • Store it in a variable
  • Print the above obtained Timedelta object
  • Apply seconds attribute on the above Timedelta object to get the number of seconds in the above Timedelta object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random Timestamp in the format(days, hours, minutes, seconds,
# milliseconds, microseconds, nanoseconds) to the Timedelta() function of 
# the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta('7 days 10:15:08.00000090') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 

# Apply seconds attribute on the above Timedelta object to get the 
# number of seconds in the above Timedelta object
print("The number of seconds in the above Timedelta object:")
print(timedelta_obj.seconds)

Output:

The above obtained Timedelta object: 7 days 10:15:08.000000900
The number of seconds in the above Timedelta object:
36908

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random Timestamp in the format(days, minutes, seconds) to the Timedelta() function of the pandas module to get the Timedelta object.
  • Store it in a variable
  • Print the above obtained Timedelta object.
  • Apply seconds attribute on the above Timedelta object to get the number of seconds in the above Timedelta object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random Timestamp in the format(days, minutes, seconds) to the Timedelta() 
# function of the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta('6 days 30 minutes 15s') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 
print()
# Apply seconds attribute on the above Timedelta object to get the 
# number of seconds in the above Timedelta object
print("The number of seconds in the above Timedelta object:")
print(timedelta_obj.seconds)

Output:

The above obtained Timedelta object: 6 days 00:30:15

The number of seconds in the above Timedelta object:
1815

Example3

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random number and unit as argument to the Timedelta() 
# function of the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta(50, unit='s') 
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj) 
print()
# Apply seconds attribute on the above Timedelta object to get the 
# number of seconds in the above Timedelta object
print("The number of seconds in the above Timedelta object:")
print(timedelta_obj.seconds)

Output:

The above obtained Timedelta object: 0 days 00:00:50

The number of seconds in the above Timedelta object:
50