Open In App

Python | Pandas TimedeltaIndex.searchsorted

Last Updated : 06 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.searchsorted() function find indices where elements should be inserted to maintain order. Find the indices into a sorted TimedeltaIndex self such that, if the corresponding elements in value were inserted before the indices, the order of self would be preserved.

Syntax : TimedeltaIndex.searchsorted(value, side=’left’, sorter=None)

Parameters :
value : [array_like] Values to insert into self.
side : If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of self)
sorter : Optional array of integer indices that sort self into ascending order. They are typically the result of np.argsort.

Return : Array of insertion points with the same shape as value.

Example #1: Use TimedeltaIndex.searchsorted() function to find indices where an element should be inserted to maintain order in the given TimedeltaIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ='11 days 22:14:12.001124', periods = 5
                                         freq ='T', name ='New_object')
  
# Print the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.searchsorted() function to find the value of the index where the given element should be inserted to maintain the order in the given object.




# return the value of index
tidx.searchsorted('11 days 22:16:13.001124')


Output :

As we can see in the output, the TimedeltaIndex.searchsorted() function has returned 3 indicating that this is place where the passed value should be inserted to maintain the order in the given TimedeltaIndex object.
 
Example #2: Use TimedeltaIndex.searchsorted() function to find indices where an element should be inserted to maintain order in the given TimedeltaIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ='03 days 09:22:56', periods = 5,
                                  freq ='H', name ='New_object')
  
# Print the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.searchsorted() function to find the value of the index where the given element should be inserted to maintain the order in the given object.




# return the value of index
tidx.searchsorted('3 days 09:45:56')


Output :

As we can see in the output, the TimedeltaIndex.searchsorted() function has returned 3 indicating that this is place where the passed value should be inserted



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads