Python Pandas DatetimeIndex.round()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas DatetimeIndex.round()函数将无时区的DatetimeIndex本地化为有时区意识的DatetimeIndex。这个方法接收一个无时区(tz)的DatetimeIndex对象,并使这个时区感知。它不会将时间转移到另一个时区。时区定位有助于从有时区意识的对象切换到无时区意识的对象。
语法: DatetimeIndex.round(freq, *args, **kwargs)
参数 :
freq : 将指数四舍五入的频率水平。必须是一个固定的频率,如’S’(第二)而不是’ME’(月末)。
返回:DatetimeIndex或TimedeltaIndex的相同类型的索引,或Series的相同索引的系列。
示例#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')
输出 :
正如我们在输出中所看到的,该函数已经将数值四舍五入到所需的频率。