Python | Панды DatetimeIndex.to_series ()

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

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

Pandas DatetimeIndex.to_series() function create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index.

Syntax: DatetimeIndex.to_series(keep_tz=False, index=None, name=None)

Parameters :
keep_tz : return the data keeping the timezone
index : index of resulting Series. If None, defaults to original index
name : name of resulting Series. If None, defaults to name of original index

Return : Series

Example #1: Use DatetimeIndex.to_series() function to create a series object from the given DatetimeIndex object. Also set the value of index for the series.

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here "S" represents secondly frequency 
didx = pd.DatetimeIndex(start ="2018-11-15 09:45:10", freq ="S", periods = 5)
  
# Print the DatetimeIndex
print(didx)

Выход :

Now we want to construct a series out of the DatetimeIndex object.

# construct the series
didx.to_series(index =["A", "B", "C", "D", "E"])

Output :

As we can see in the output, the function has returned a series object constructed from the didx DatetimeIndex object.
 
Example #2: Use DatetimeIndex.to_series() function to create a series object from the given DatetimeIndex object. Also set the value of index for the series.

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here "M" represents monthly frequency 
didx = pd.DatetimeIndex(start ="2015-03-02", freq ="M", periods = 5)
  
# Print the DatetimeIndex
print(didx)

Выход :

Now we want to construct a series out of the DatetimeIndex object.

# construct the series
didx.to_series(index =["First", "Second", "Third", "Fourth", "Fifth"])

Выход :

Как видно из выходных данных, функция вернула объект серии, созданный из объекта didx DatetimeIndex.

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

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