Python Pandas DatetimeIndex.floor()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas DatetimeIndex.floor()函数将数据下调到指定频率。该函数将目标频率作为输入。它返回一个新的DatetimeIndex对象。
语法: DatetimeIndex.floor(freq)
参数 :
freq : 指数下限的频率水平。必须是一个固定的频率,如’S’(第二)而不是’ME’(月末)。
返回:DatetimeIndex或TimedeltaIndex的相同类型的索引,或Series的相同索引的系列。
示例#1:使用DatetimeIndex.floor()函数将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.floor('T')
输出 :
正如我们在输出中看到的,该函数已经将DatetimeIndex对象的值浮动到所需的频率。
示例#2:使用DatetimeIndex.floor()函数将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对象的基于分钟的频率降到基于小时的频率。
# floor minute based frequency to hour based frequency
didx.floor('H')
输出 :
正如我们在输出中看到的,该函数已经将DatetimeIndex对象的值浮动到所需的频率。