Python | Панды TimedeltaIndex.slice_indexer

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

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

Pandas TimedeltaIndex.slice_indexer() function compute the slice indexer for input labels and step for an ordered TimedeltaIndex object. The function assumes that the data is sorted.

Syntax : TimedeltaIndex.slice_indexer(start=None, end=None, step=None, kind=None)

Parameters :
start : If None, defaults to the beginning
end : If None, defaults to the end
step : int, default None
kind : string, default None

Return : indexer : ndarray or slice

Example #1: Use TimedeltaIndex.slice_indexer() function to compute the slice indexer for the passed label 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")
  
# Print the TimedeltaIndex object
print(tidx)

Выход :

Now we will use the TimedeltaIndex.slice_indexer() function to find the slice value for the passed labels.

# find the slice indexer
tidx.slice_indexer("11 days 22:15:20.001124")

Выход :

As we can see in the output, the TimedeltaIndex.slice_indexer() function has returned the position as well as the number of elements in the tidx object.
 
Example #2: Use TimedeltaIndex.slice_indexer() function to compute the slice indexer for the passed label 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")
  
# Print the TimedeltaIndex object
print(tidx)

Выход :

Now we will use the TimedeltaIndex.slice_indexer() function to find the slice value for the passed labels.

# find the slice indexer
tidx.slice_indexer("3 days 12:20:56")

Output :

As we can see in the output, the TimedeltaIndex.slice_indexer() function has returned the position as well as the number of elements in the tidx object.

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

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