Python Pandas - 从具有特定时间序列频率的 DateTimeIndex 中提取微秒
要从具有特定时间序列频率的 DateTimeIndex 中提取微秒,请使用 **DateTimeIndex.microsecond** 属性。
首先,导入所需的库 -
import pandas as pd
创建一个周期为 6 且频率为 us(即微秒)的 DatetimeIndex。时区为澳大利亚/悉尼 -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='us')
显示 DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex)
获取微秒 -
print("\nGetting the microseconds..\n",datetimeindex.microsecond)
示例
以下是代码 -
import pandas as pd # DatetimeIndex with period 6 and frequency as us i.e. microseconds # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='us') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # get the microsecond print("\nGetting the microseconds..\n",datetimeindex.microsecond)
输出
这将生成以下代码 -
DateTimeIndex... DatetimeIndex([ '2021-10-20 02:30:50+11:00', '2021-10-20 02:30:50.000001+11:00', '2021-10-20 02:30:50.000002+11:00', '2021-10-20 02:30:50.000003+11:00', '2021-10-20 02:30:50.000004+11:00', '2021-10-20 02:30:50.000005+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='U') DateTimeIndex frequency... <Micro> Getting the microseconds.. Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')
广告