Python Pandas - 从 DateTimeIndex 提取频率
要从 DateTimeIndex 中提取频率,在 Pandas 中使用 DateTimeIndex.freq 属性。
首先,导入所需的库 −
import pandas as pd
创建一个具有周期 6 和频率为 D(即天)的 DatetimeIndex。时区是 Australia/Adelaide −
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')
显示 DateTimeIndex −
print("DateTimeIndex...\n", datetimeindex)
显示 DateTimeIndex 频率 −
print("DateTimeIndex frequency...\n", datetimeindex.freq)
示例
以下为代码 −
import pandas as pd # DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq)
输出
这将生成以下代码 −
DateTimeIndex... DatetimeIndex(['2021-10-20 02:30:50+10:30', '2021-10-21 02:30:50+10:30', '2021-10-22 02:30:50+10:30', '2021-10-23 02:30:50+10:30', '2021-10-24 02:30:50+10:30', '2021-10-25 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='D') DateTimeIndex frequency... <Day>
广告