📜  Python|熊猫 DatetimeIndex.to_series()

📅  最后修改于: 2022-05-13 01:54:37.112000             🧑  作者: Mango

Python|熊猫 DatetimeIndex.to_series()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas DatetimeIndex.to_series()函数创建一个系列,其索引和值都等于索引键,可用于基于索引返回索引器的映射。

示例 #1:使用DatetimeIndex.to_series()函数从给定的 DatetimeIndex 对象创建一个系列对象。还要设置系列的索引值。

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

输出 :

现在我们要从 DatetimeIndex 对象中构造一个系列。

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

输出 :

正如我们在输出中看到的,该函数返回了一个从didx DatetimeIndex 对象构造的系列对象。示例 #2:使用DatetimeIndex.to_series()函数从给定的 DatetimeIndex 对象创建一个系列对象。还要设置系列的索引值。

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

输出 :

现在我们要从 DatetimeIndex 对象中构造一个系列。

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

输出 :

正如我们在输出中看到的,该函数返回了一个从didx DatetimeIndex 对象构造的系列对象。