Python | Панды TimedeltaIndex.union ()

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

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

Pandas TimedeltaIndex.union() function is a specialized union for TimedeltaIndex objects. It combine overlapping ranges with the same DateOffset, will be much faster than Index.union

Syntax : TimedeltaIndex.union(other)

Parameters :
other : TimedeltaIndex or array-like

Return : Index or TimedeltaIndex

Example #1: Use TimedeltaIndex.union() function to find the set union of two TimedeltaIndex objects.

# 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 seccond TimedeltaIndex object
tidx2 = pd.TimedeltaIndex(start ="11 days 22:14:12.001124",
                         periods = 5, freq ="T", name ="New_object")
  
# Print the first TimedeltaIndex object
print(tidx1)
  
# Print the second TimedeltaIndex object
print(tidx2)

Выход :

Now we will use the TimedeltaIndex.union() function to find the set union of the two objects

# find the union
tidx1.union(tidx2)

Выход :

As we can see in the output, the TimedeltaIndex.union() function has returned an object which contains the union of both tidx1 and tidx2.
 
Example #2: Use TimedeltaIndex.union() function to find the set union of two TimedeltaIndex objects.

# 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 TimedeltaIndex object
print(tidx1)
  
# Print the second TimedeltaIndex object
print(tidx2)

Выход :

Now we will use the TimedeltaIndex.union() function to find the set union of the two objects

# find the union
tidx1.union(tidx2)

Output :

As we can see in the output, the TimedeltaIndex.union() function has returned an object which contains the union of both tidx1 and tidx2.

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

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