Python Pandas - 从 DateTimeIndex 中提取频率对象作为字符串
要从DateTimeIndex中以字符串形式提取频率对象,请使用Pandas中的DateTimeIndex.freqstr属性。
首先,导入所需的库-
import pandas as pd
创建一个周期为6且频率为D的DatetimeIndex-
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')
显示日期时间索引-
print("DateTimeIndex...\n", datetimeindex)
将DateTimeIndex频率显示为字符串-
print("DateTimeIndex frequency as string...\n", datetimeindex.freqstr)
示例
以下是代码-
import pandas as pd #DatetimeIndex,周期为6,频率为Dieday #时区是澳大利亚/阿德莱德 datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') #显示日期时间索引 print("DateTimeIndex...\n", datetimeindex) #显示日期时间索引 frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) #显示日期时间索引 frequency as a string print("DateTimeIndex frequency as string...\n", datetimeindex.freqstr)输出结果
这将产生以下代码-
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> DateTimeIndex frequency as string... D