📜  Python|熊猫 DatetimeIndex.round()

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

Python|熊猫 DatetimeIndex.round()

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

Pandas DatetimeIndex.round()函数将 tz-naive DatetimeIndex 本地化为 tz-aware DatetimeIndex。此方法采用时区 (tz) 天真的 DatetimeIndex 对象,并使该时区感知。它不会将时间移动到另一个时区。时区本地化有助于从时区感知对象切换到时区不感知对象。

示例#1:使用DatetimeIndex.round()函数将 DatetimeIndex 对象的数据四舍五入到指定的频率。

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'S' represents secondly frequency 
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', 
                           freq ='S', periods = 4)
  
# Print the DatetimeIndex
print(didx)

输出 :

现在我们要将 DatetimeIndex 对象的基于秒的频率转换为基于分钟的频率

# convert to the passed frequency
# 'T' represents minute based frequency
didx.round(freq ='T')

输出 :

正如我们在输出中看到的那样,该函数已将值四舍五入到所需的频率。示例#2:使用DatetimeIndex.round()函数将 DatetimeIndex 对象的数据四舍五入到指定的频率。

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'T' represents minutely frequency 
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', 
                           freq ='T', periods = 4)
  
# Print the DatetimeIndex
print(didx)

输出 :

现在我们要将 DatetimeIndex 对象的基于分钟的频率转换为基于小时的频率

# convert to the passed frequency
# Convert minute based frequency to hour based frequency
didx.round(freq ='H')

输出 :

正如我们在输出中看到的那样,该函数已将值四舍五入到所需的频率。