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.