Python Pandas - 使用特定时间序列频率从 DateTimeIndex 提取一年中的顺序日


若要使用特定时间序列频率从 DateTimeIndex 提取一年中的顺序日,请使用 DateTimeIndex.dayofyear 属性

首先,导入所需的库 −

import pandas as pd

使用周期 6 和频率为 D(即天)创建 DateTimeIndex −

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

展示 DateTimeIndex −

print("DateTimeIndex...\n", datetimeindex)

获取一年的某一天 −

print("\nGet the ordinal day of year..\n",datetimeindex.dayofyear)

示例

以下是代码 −

import pandas as pd

# DatetimeIndex with period 6 and frequency as D i.e. day
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# get the day of year
print("\nGet the ordinal day of year..\n",datetimeindex.dayofyear)

输出

这会产生以下代码 −

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-21 02:30:50+11:00',
'2021-10-22 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
'2021-10-24 02:30:50+11:00', '2021-10-25 02:30:50+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='D')
DateTimeIndex frequency...
<Day>

Get the ordinal day of year..
Int64Index([293, 294, 295, 296, 297, 298], dtype='int64')

更新时间:18-Oct-2021

266 次浏览

开启你的职业

完成课程获得认证

开始
广告
© . All rights reserved.