Python Pandas - 检测给定的 DatetimeIndex 对象的频率
若要检测给定的 DatetimeIndex 对象的频率,可使用 DateTimeIndex.inferred_freq 属性。
首先,导入所需的库 −
import pandas as pd
创建一个周期为 5,频率为 Y(即年)的 DatetimeIndex。时区设为澳大利亚/阿德莱德 −
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='3Y')
显示 DateTimeIndex −
print("DateTimeIndex...\n", datetimeindex)
显示 DateTimeIndex 频率 −
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
示例
以下是代码 −
import pandas as pd # DatetimeIndex with period 5 and frequency as Y i.e. years # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='3Y') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("\nDateTimeIndex frequency...\n", datetimeindex.freq) # detect the frequency print("\nInferred DateTimeIndex frequency...\n", datetimeindex.inferred_freq)
输出
将会产生以下代码 −
DateTimeIndex... DatetimeIndex(['2021-12-31 02:30:50+10:30', '2024-12-31 02:30:50+10:30', '2027-12-31 02:30:50+10:30', '2030-12-31 02:30:50+10:30', '2033-12-31 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='3A-DEC') DateTimeIndex frequency... <3 * YearEnds: month=12> Inferred DateTimeIndex frequency... 3A-DEC
广告