TSAP(2) : 时区切换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014281392/article/details/83188181

TSAP : TimeSeries Analysis with Python

import numpy as np
import pandas as pd
pd.date_range('3/6/2012 00:00', periods = 15, freq = 'D')

DatetimeIndex([‘2012-03-06’, ‘2012-03-07’, ‘2012-03-08’, ‘2012-03-09’,
‘2012-03-10’, ‘2012-03-11’, ‘2012-03-12’, ‘2012-03-13’,
‘2012-03-14’, ‘2012-03-15’, ‘2012-03-16’, ‘2012-03-17’,
‘2012-03-18’, ‘2012-03-19’, ‘2012-03-20’],
dtype=‘datetime64[ns]’, freq=‘D’)

# Europe/London 时区
pd.date_range('3/6/2012 00:00', periods = 15, freq = 'D', tz = 'Europe/London')

DatetimeIndex([‘2012-03-06’, ‘2012-03-07’, ‘2012-03-08’, ‘2012-03-09’,
‘2012-03-10’, ‘2012-03-11’, ‘2012-03-12’, ‘2012-03-13’,
‘2012-03-14’, ‘2012-03-15’, ‘2012-03-16’, ‘2012-03-17’,
‘2012-03-18’, ‘2012-03-19’, ‘2012-03-20’],
dtype=‘datetime64[ns, Europe/London]’, freq=‘D’)

from pytz import common_timezones, all_timezones
print(len(common_timezones))
print(len(all_timezones))
print(set(all_timezones) - set(common_timezones))
439
592
{'Etc/GMT+8', 'Etc/GMT-5', 'MST', 'America/Atka', 'Australia/Queensland', 'Atlantic/Jan_Mayen', 'America/Louisville', 'America/Catamarca', 'Etc/GMT-8', 'Etc/GMT', 'NZ-CHAT', 'Etc/UCT', 'Etc/Zulu', 'Mexico/BajaSur', 'Europe/Nicosia', 'ROC', 'Egypt', 'Etc/GMT+4', 'Cuba', 'GMT+0', 'Africa/Asmera', 'Antarctica/South_Pole', 'Jamaica', 'US/Michigan', 'Asia/Katmandu', 'Iran', 'America/Montreal', 'Etc/GMT-1', 'PST8PDT', 'Pacific/Samoa', 'Mexico/BajaNorte', 'Etc/GMT0', 'Etc/GMT-12', 'America/Porto_Acre', 'Universal', 'US/Pacific-New', 'Asia/Thimbu', 'GB', 'Etc/GMT+6', 'Eire', 'Australia/South', 'Turkey', 'Australia/North', 'Etc/GMT-2', 'Hongkong', 'Etc/GMT-10', 'Etc/GMT+3', 'Etc/GMT+9', 'Atlantic/Faeroe', 'Etc/GMT-6', 'Asia/Kashgar', 'America/Coral_Harbour', 'Etc/GMT-7', 'America/Virgin', 'Europe/Tiraspol', 'Asia/Ujung_Pandang', 'HST', 'Japan', 'EET', 'UCT', 'Kwajalein', 'GMT-0', 'CET', 'Asia/Dacca', 'America/Cordoba', 'Asia/Saigon', 'GB-Eire', 'America/Ensenada', 'Chile/EasterIsland', 'Portugal', 'America/Knox_IN', 'Etc/GMT+11', 'Israel', 'Asia/Istanbul', 'Etc/GMT+5', 'Asia/Chungking', 'Etc/GMT+2', 'Navajo', 'US/Aleutian', 'Brazil/DeNoronha', 'Canada/Yukon', 'America/Jujuy', 'Australia/ACT', 'Asia/Ashkhabad', 'Australia/Tasmania', 'Australia/Canberra', 'MST7MDT', 'Brazil/Acre', 'US/East-Indiana', 'Etc/GMT+7', 'EST', 'America/Fort_Wayne', 'Greenwich', 'Asia/Rangoon', 'WET', 'EST5EDT', 'US/Samoa', 'Mexico/General', 'Chile/Continental', 'Etc/GMT+10', 'NZ', 'Pacific/Ponape', 'ROK', 'Poland', 'Asia/Calcutta', 'America/Indianapolis', 'Asia/Ulan_Bator', 'US/Indiana-Starke', 'Etc/GMT-13', 'Etc/GMT-4', 'Brazil/West', 'Etc/GMT+12', 'Etc/Universal', 'Canada/Saskatchewan', 'CST6CDT', 'Pacific/Yap', 'America/Mendoza', 'Etc/GMT-3', 'Australia/Victoria', 'Europe/Belfast', 'Africa/Timbuktu', 'America/Buenos_Aires', 'MET', 'America/Rosario', 'Pacific/Johnston', 'Etc/UTC', 'Singapore', 'Australia/NSW', 'Brazil/East', 'Australia/LHI', 'America/Shiprock', 'Etc/GMT-9', 'GMT0', 'PRC', 'W-SU', 'Asia/Harbin', 'Etc/GMT+0', 'Libya', 'Asia/Macao', 'Australia/West', 'Pacific/Truk', 'Zulu', 'Australia/Yancowinna', 'America/Santa_Isabel', 'Etc/GMT-0', 'Etc/GMT-14', 'Etc/GMT+1', 'America/Argentina/ComodRivadavia', 'Iceland', 'Etc/Greenwich', 'Asia/Chongqing', 'Etc/GMT-11', 'Asia/Tel_Aviv'}

时区转换

# You can also localize a naive timestamp
t_naive = pd.Timestamp('2016-07-10 08:50')
# 指定本地时区
t = t_naive.tz_localize(tz = 'US/Central')
# 转换为Asia/Chongqing
t.tz_convert('Asia/Chongqing')

Timestamp(‘2016-07-10 21:50:00+0800’, tz=‘Asia/Chongqing’)

# how to handle daylight savings?
rng = pd.date_range('2016-03-10', periods=10, tz='US/Central')
ts = pd.Series(range(10), index=rng)
ts

2016-03-10 00:00:00-06:00 0
2016-03-11 00:00:00-06:00 1
2016-03-12 00:00:00-06:00 2
2016-03-13 00:00:00-06:00 3
2016-03-14 00:00:00-05:00 4
2016-03-15 00:00:00-05:00 5
2016-03-16 00:00:00-05:00 6
2016-03-17 00:00:00-05:00 7
2016-03-18 00:00:00-05:00 8
2016-03-19 00:00:00-05:00 9
Freq: D, dtype: int64

rng = pd.date_range('2016-03-10', periods=10, tz='utc')
ts = pd.Series(range(10), index=rng)
ts

2016-03-10 00:00:00+00:00 0
2016-03-11 00:00:00+00:00 1
2016-03-12 00:00:00+00:00 2
2016-03-13 00:00:00+00:00 3
2016-03-14 00:00:00+00:00 4
2016-03-15 00:00:00+00:00 5
2016-03-16 00:00:00+00:00 6
2016-03-17 00:00:00+00:00 7
2016-03-18 00:00:00+00:00 8
2016-03-19 00:00:00+00:00 9
Freq: D, dtype: int64

ts.tz_convert('US/Central')

2016-03-09 18:00:00-06:00 0
2016-03-10 18:00:00-06:00 1
2016-03-11 18:00:00-06:00 2
2016-03-12 18:00:00-06:00 3
2016-03-13 19:00:00-05:00 4
2016-03-14 19:00:00-05:00 5
2016-03-15 19:00:00-05:00 6
2016-03-16 19:00:00-05:00 7
2016-03-17 19:00:00-05:00 8
2016-03-18 19:00:00-05:00 9
Freq: D, dtype: int64

pd.date_range('03-12-2016 22:00', periods = 12, freq = 'H', tz = 'US/Eastern')

DatetimeIndex([‘2016-03-12 22:00:00-05:00’, ‘2016-03-12 23:00:00-05:00’,
‘2016-03-13 00:00:00-05:00’, ‘2016-03-13 01:00:00-05:00’,
‘2016-03-13 03:00:00-04:00’, ‘2016-03-13 04:00:00-04:00’,
‘2016-03-13 05:00:00-04:00’, ‘2016-03-13 06:00:00-04:00’,
‘2016-03-13 07:00:00-04:00’, ‘2016-03-13 08:00:00-04:00’,
‘2016-03-13 09:00:00-04:00’, ‘2016-03-13 10:00:00-04:00’],
dtype=‘datetime64[ns, US/Eastern]’, freq=‘H’)

猜你喜欢

转载自blog.csdn.net/u014281392/article/details/83188181