Python Pandas - 使用特定时间序列频率从 DateTimeIndex 中提取时区


若要从 DateTimeIndex 中提取特定时间序列频率的时区,请使用 DateTimeIndex.tz 属性。

首先,导入必需的库 -

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 timezone..\n",datetimeindex.tz)

示例

以下为代码 -

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 timezone
print("\nGet the timezone..\n",datetimeindex.tz)

输出

这将生成以下代码 -

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 timezone..
Australia/Sydney

更新于: 2021-10-18

1K+ 浏览

开启您的 事业

完成课程即可获得认证

开始吧
广告
© . All rights reserved.