当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python Pandas DatetimeIndex.to_series()用法及代码示例

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas DatetimeIndex.to_series()函数使用索引和等于索引键的值创建一个Series,该索引对map有用,用于基于索引返回索引器。

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

参数:
keep_tz:返回保留时区的数据
index:所得系列的索引。如果为None,则默认为原始索引
name:所得系列的名称。如果为None,则默认为原始索引的名称

返回:系列

范例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对象构造的系列对象。



相关用法


注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas DatetimeIndex.to_series()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。