Sunday, October 5, 2025
HomeLanguagesPython | Pandas TimedeltaIndex.searchsorted

Python | Pandas TimedeltaIndex.searchsorted

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11934 POSTS0 COMMENTS
Shaida Kate Naidoo
6822 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS