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("\n获取微秒..\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("\n获取微秒..\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>
获取微秒..
Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')