Pandas Timedelta.components Function:
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.
Timedelta.components attribute in pandas.Timedelta module is used to give a Components NamedTuple-like.
Syntax:
Timedelta.components
Parameters: It has no arguments
Return Value:
All the Components of the given Timestamp format are returned as a tuple.
The Components NamedTuple-like are returned by the Timedelta.components attribute of the Pandas module.
Pandas Timedelta.components Function 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 components attribute on the above Timedelta object to get all the components of the given Timedelta object as a tuple.
-
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.13010')
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj)
# Apply components attribute on the above Timedelta object to get all the
# components of the given Timedelta object as a tuple.
print("The components of the given Timedelta object as a tuple:")
print(timedelta_obj.components)
Output:
The above obtained Timedelta object: 7 days 10:15:08.130100 The components of the given Timedelta object as a tuple: Components(days=7, hours=10, minutes=15, seconds=8, milliseconds=130, microseconds=100, nanoseconds=0)
Example2
Here only days, minutes are passed as arguments. so, all the other components like hours, seconds, etc are set to 0.
Approach:
- Import pandas module using the import keyword.
- Pass some random Timestamp in the format(days, minutes) 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 components attribute on the above Timedelta object to get all the components of the given Timedelta object as a tuple.
-
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) 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')
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj)
# Apply components attribute on the above Timedelta object to get all the
# components of the given Timedelta object as a tuple.
print("The components of the given Timedelta object as a tuple:")
print(timedelta_obj.components)
Output:
The above obtained Timedelta object: 6 days 00:30:00 The components of the given Timedelta object as a tuple: Components(days=6, hours=0, minutes=30, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0)
Example3
We can also pass the Timestamp format as shown below:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random Timestamp in the format(days, minutes, milliseconds) to the Timedelta()
# function of the pandas module to get the Timedelta object.
# Store it in a variable
timedelta_obj = pd.Timedelta(days=12, minutes=15, milliseconds=20)
# Print the above obtained Timedelta object
print("The above obtained Timedelta object:", timedelta_obj)
# Apply components attribute on the above Timedelta object to get all the
# components of the given Timedelta object as a tuple.
print("The components of the given Timedelta object as a tuple:")
print(timedelta_obj.components)
Output:
The above obtained Timedelta object: 12 days 00:15:00.020000 The components of the given Timedelta object as a tuple: Components(days=12, hours=0, minutes=15, seconds=0, milliseconds=20, microseconds=0, nanoseconds=0)