Python Pandas - 在 DateTimeIndex 中返回一天中特定时间值的索引位置
要在DateTimeIndex中返回一天中特定时间值的索引位置,请使用该方法。DateTimeIndex.indexer_at_time()
首先,导入所需的库-
import pandas as pd
创建日期时间索引,周期为5,频率为T即分钟-
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')显示日期时间索引-
print("DateTimeIndex...\n", datetimeindex)在一天中的特定时间显示值的索引位置,即此处的03:10:50-
print("\nIndex locations of values at particular time of day...\n",
datetimeindex.indexer_at_time('2021-10-30 03:10:50'))示例
以下是代码-
import pandas as pd
#DatetimeIndexwithperiod5andfrequencyasTi.e.minutes
#ThetimezoneisAustralia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
#displayDateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
#displayDateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
#displayindexlocationsofvaluesatparticulartimeofdayi.e.03:10:50here
print("\nIndex locations of values at particular time of day...\n",
datetimeindex.indexer_at_time('2021-10-30 03:10:50'))输出结果这将产生以下代码-
DateTimeIndex... DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 02:50:50+10:30', '2021-10-30 03:10:50+10:30', '2021-10-30 03:30:50+10:30', '2021-10-30 03:50:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='20T') DateTimeIndex frequency... <20 * Minutes> Index locations of values at particular time of day... [2]
热门推荐
2 十月简短祝福语
10 给婴儿祝福语简短的话
11 万寿祝福语简短
12 朋友孩子考试祝福语简短
13 孩子16岁祝福语简短
14 端午老师祝福语简短优美
15 钱包祝福语简短10字
16 小红书平安祝福语简短
17 年末祝福语简短的文案
18 老师送的祝福语简短