Python | Панды TimedeltaIndex.searchsorted

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

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)

Выход :

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")

Выход :

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)

Выход :

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

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.