Python Pandas - 将 DateTimeIndex 中的时间戳捕捉到最近的出现频率
要将DateTimeIndex中的时间戳捕捉到最近的出现频率,请使用该方法。使用freq参数设置频率。DateTimeIndex.snap()
首先,导入所需的库-
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)将时间戳捕捉到最近发生的事件,即这里的月末-
print("\nSnap time stamps to nearest occurring frequency...\n",
datetimeindex.snap(freq='M'))示例
以下是代码-
import pandas as pd
#DatetimeIndexwithperiod6andfrequencyasDi.e.day
#ThetimezoneisAustralia/Adelaide
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')
#displayDateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
#displayDateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
#Snaptimestampstonearestoccurringi.e.Monthendhere
print("\nSnap time stamps to nearest occurring frequency...\n",
datetimeindex.snap(freq='M'))输出结果这将产生以下代码-
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> Snap time stamps to nearest occurring frequency... DatetimeIndex(['2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
热门推荐
10 情侣祝福语古文简短独特
11 酒店伴娘祝福语简短
12 晚上甜蜜的祝福语简短
13 给新娘简短的祝福语
14 当爸爸的简短祝福语
15 洛宁新年祝福语大全简短
16 新年祝福语简短搞笑幽默
17 结婚祝福语大全简短霸气
18 新人结婚祝福语简短高端