Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
 Pandas  DatetimeIndex.snap()函数用于将时间戳捕捉到最近发生的频率。该函数采用一个参数,即我们在捕捉DatetimeIndex对象的时间戳值时要应用的频率。
用法: DatetimeIndex.snap(freq)
参数:
freq:频率
返回:日期时间索引
范例1:采用DatetimeIndex.snap()函数根据输入频率将给定的DatetimeIndex对象转换为最近的发生频率。
# importing pandas as pd 
import pandas as pd 
  
# Create the DatetimeIndex 
# Here 'Q' represents quarter end frequency  
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q', 
                          periods = 4, tz ='Asia/Calcutta') 
  
# Print the DatetimeIndex 
print(didx)输出:

现在,我们要基于输入将给定的DatetimeIndex对象时间戳值转换为最接近的频率。
# snap the timestamp to the nearest frequency  
didx.snap('MS')输出:

从输出中可以看到,该函数已将给定的DatetimeIndex对象中的每个时间戳值对齐。
范例2:采用DatetimeIndex.snap()函数根据输入频率将给定的DatetimeIndex对象转换为最近的发生频率。
# importing pandas as pd 
import pandas as pd 
  
# Create the DatetimeIndex 
# Here 'MS' represents month start frequency  
didx = pd.date_range(pd.Timestamp("2000-01-15 08:00"),  
                              periods = 5, freq ='MS') 
  
# Print the DatetimeIndex 
print(didx)输出:

现在,我们要基于输入将给定的DatetimeIndex对象时间戳值转换为最接近的频率。
# snap the timestamp to the nearest frequency  
didx.snap('Q')输出:

从输出中可以看到,该函数已将给定的DatetimeIndex对象中的每个时间戳值对齐。
相关用法
- Python pandas.map()用法及代码示例
 - Python Pandas Series.str.len()用法及代码示例
 - Python Pandas.factorize()用法及代码示例
 - Python Pandas TimedeltaIndex.name用法及代码示例
 - Python Pandas dataframe.ne()用法及代码示例
 - Python Pandas Series.between()用法及代码示例
 - Python Pandas DataFrame.where()用法及代码示例
 - Python Pandas Series.add()用法及代码示例
 - Python Pandas.pivot_table()用法及代码示例
 - Python Pandas Series.mod()用法及代码示例
 - Python Pandas Dataframe.at[ ]用法及代码示例
 - Python Pandas Dataframe.iat[ ]用法及代码示例
 - Python Pandas.pivot()用法及代码示例
 - Python Pandas dataframe.mul()用法及代码示例
 - Python Pandas.melt()用法及代码示例
 
注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas DatetimeIndex.snap()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
