Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas TimedeltaIndex.intersection() function return a new Index with elements from the index that are common to both the indexes. This is a specialized intersection for TimedeltaIndex objects. It may be much faster than Index.intersection.
Syntax : TimedeltaIndex.intersection(other)
Parameters :Â
other : TimedeltaIndex or array-like
Return : Index or TimedeltaIndex
Â
Example #1: Use TimedeltaIndex.intersection() function to find the intersection of two TimedeltaIndex objects.Â
Python3
# importing pandas as pd import pandas as pd Â
# Create the first TimedeltaIndex object tidx1 = pd.TimedeltaIndex(start = '11 days 22:11:12.001124' , periods = 5 , Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â freq = 'T' , name = 'New_object' ) Â
# Create the second TimedeltaIndex object tidx2 = pd.TimedeltaIndex(start = '11 days 22:14:12.001124' , periods = 5 , Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â freq = 'T' , name = 'New_object' ) Â
# Print the first and second TimedeltaIndex object print (tidx1, '\n' , tidx2) |
Output :Â
Â
Â
Now we will use the TimedeltaIndex.intersection() function to find the intersection of the two objects
Python3
# find the intersection tidx1.intersection(tidx2) |
Output :Â
Â
As we can see in the output, the TimedeltaIndex.intersection() function has returned an object which contains only those elements which are common to both tidx1 and tidx2.Â
 Â
Example #2: Use TimedeltaIndex.intersection() function to find the intersection of two TimedeltaIndex objects.
Python3
# importing pandas as pd import pandas as pd Â
# Create the first TimedeltaIndex object tidx1 = pd.TimedeltaIndex(start = '1 days 02:00:12.001124' , Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â periods = 5 , freq = 'D' , name = 'Koala' ) Â
# Create the second TimedeltaIndex object tidx2 = pd.TimedeltaIndex(start = '3 days 02:00:12.001124' , Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â periods = 5 , freq = 'D' , name = 'Koala' ) Â
# Print the first and second TimedeltaIndex object print (tidx1, '\n' , tidx2) |
Output :Â
Â
Â
Now we will use the TimedeltaIndex.intersection() function to find the intersection of the two objects
Python3
# find the intersection tidx1.intersection(tidx2) |
Output :Â
Â
As we can see in the output, the TimedeltaIndex.intersection() function has returned an object which contains only those elements which are common to both tidx1 and tidx2.
Â