本文整理汇总了Python中pytz.NonExistentTimeError方法的典型用法代码示例。如果您正苦于以下问题:Python pytz.NonExistentTimeError方法的具体用法?Python pytz.NonExistentTimeError怎么用?Python pytz.NonExistentTimeError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytz
的用法示例。
在下文中一共展示了pytz.NonExistentTimeError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dti_tz_localize_nonexistent_raise_coerce
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize_nonexistent_raise_coerce(self):
# GH#13057
times = ['2015-03-08 01:00', '2015-03-08 02:00', '2015-03-08 03:00']
index = DatetimeIndex(times)
tz = 'US/Eastern'
with pytest.raises(pytz.NonExistentTimeError):
index.tz_localize(tz=tz)
with pytest.raises(pytz.NonExistentTimeError):
with tm.assert_produces_warning(FutureWarning):
index.tz_localize(tz=tz, errors='raise')
with tm.assert_produces_warning(FutureWarning,
clear=FutureWarning,
check_stacklevel=False):
result = index.tz_localize(tz=tz, errors='coerce')
test_times = ['2015-03-08 01:00-05:00', 'NaT',
'2015-03-08 03:00-04:00']
dti = to_datetime(test_times, utc=True)
expected = dti.tz_convert('US/Eastern')
tm.assert_index_equal(result, expected)
示例2: test_dti_tz_localize_ambiguous_times
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize_ambiguous_times(self, tz):
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
freq=pd.offsets.Hour())
with pytest.raises(pytz.NonExistentTimeError):
dr.tz_localize(tz)
# after dst transition, it works
dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
freq=pd.offsets.Hour(), tz=tz)
# November 6, 2011, fall back, repeat 2 AM hour
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
freq=pd.offsets.Hour())
with pytest.raises(pytz.AmbiguousTimeError):
dr.tz_localize(tz)
# UTC is OK
dr = date_range(datetime(2011, 3, 13), periods=48,
freq=pd.offsets.Minute(30), tz=pytz.utc)
示例3: test_dti_tz_localize
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize(self, prefix):
tzstr = prefix + 'US/Eastern'
dti = pd.date_range(start='1/1/2005', end='1/1/2005 0:00:30.256',
freq='L')
dti2 = dti.tz_localize(tzstr)
dti_utc = pd.date_range(start='1/1/2005 05:00',
end='1/1/2005 5:00:30.256', freq='L', tz='utc')
tm.assert_numpy_array_equal(dti2.values, dti_utc.values)
dti3 = dti2.tz_convert(prefix + 'US/Pacific')
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
dti = pd.date_range(start='11/6/2011 1:59', end='11/6/2011 2:00',
freq='L')
with pytest.raises(pytz.AmbiguousTimeError):
dti.tz_localize(tzstr)
dti = pd.date_range(start='3/13/2011 1:59', end='3/13/2011 2:00',
freq='L')
with pytest.raises(pytz.NonExistentTimeError):
dti.tz_localize(tzstr)
示例4: test_dti_tz_localize_utc_conversion
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize_utc_conversion(self, tz):
# Localizing to time zone should:
# 1) check for DST ambiguities
# 2) convert to UTC
rng = date_range('3/10/2012', '3/11/2012', freq='30T')
converted = rng.tz_localize(tz)
expected_naive = rng + pd.offsets.Hour(5)
tm.assert_numpy_array_equal(converted.asi8, expected_naive.asi8)
# DST ambiguity, this should fail
rng = date_range('3/11/2012', '3/12/2012', freq='30T')
# Is this really how it should fail??
with pytest.raises(pytz.NonExistentTimeError):
rng.tz_localize(tz)
示例5: test_dt_round_tz_nonexistent
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dt_round_tz_nonexistent(self, method, ts_str, freq):
# GH 23324 round near "spring forward" DST
s = Series([pd.Timestamp(ts_str, tz='America/Chicago')])
result = getattr(s.dt, method)(freq, nonexistent='shift_forward')
expected = Series(
[pd.Timestamp('2018-03-11 03:00:00', tz='America/Chicago')]
)
tm.assert_series_equal(result, expected)
result = getattr(s.dt, method)(freq, nonexistent='NaT')
expected = Series([pd.NaT]).dt.tz_localize(result.dt.tz)
tm.assert_series_equal(result, expected)
with pytest.raises(pytz.NonExistentTimeError,
match='2018-03-11 02:00:00'):
getattr(s.dt, method)(freq, nonexistent='raise')
示例6: test_dti_tz_localize_nonexistent_raise_coerce
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize_nonexistent_raise_coerce(self):
# GH#13057
times = ['2015-03-08 01:00', '2015-03-08 02:00', '2015-03-08 03:00']
index = DatetimeIndex(times)
tz = 'US/Eastern'
with pytest.raises(pytz.NonExistentTimeError):
index.tz_localize(tz=tz)
with pytest.raises(pytz.NonExistentTimeError):
index.tz_localize(tz=tz, errors='raise')
result = index.tz_localize(tz=tz, errors='coerce')
test_times = ['2015-03-08 01:00-05:00', 'NaT',
'2015-03-08 03:00-04:00']
dti = DatetimeIndex(test_times)
expected = dti.tz_localize('UTC').tz_convert('US/Eastern')
tm.assert_index_equal(result, expected)
示例7: test_dti_tz_localize
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize(self, prefix):
tzstr = prefix + 'US/Eastern'
dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
freq='L')
dti2 = dti.tz_localize(tzstr)
dti_utc = DatetimeIndex(start='1/1/2005 05:00',
end='1/1/2005 5:00:30.256', freq='L', tz='utc')
tm.assert_numpy_array_equal(dti2.values, dti_utc.values)
dti3 = dti2.tz_convert(prefix + 'US/Pacific')
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
dti = DatetimeIndex(start='11/6/2011 1:59', end='11/6/2011 2:00',
freq='L')
with pytest.raises(pytz.AmbiguousTimeError):
dti.tz_localize(tzstr)
dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
freq='L')
with pytest.raises(pytz.NonExistentTimeError):
dti.tz_localize(tzstr)
示例8: test_tz_localize_dti
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_tz_localize_dti(self):
from pandas.tseries.offsets import Hour
dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
freq='L')
dti2 = dti.tz_localize('US/Eastern')
dti_utc = DatetimeIndex(start='1/1/2005 05:00',
end='1/1/2005 5:00:30.256', freq='L',
tz='utc')
self.assert_(np.array_equal(dti2.values, dti_utc.values))
dti3 = dti2.tz_convert('US/Pacific')
self.assert_(np.array_equal(dti3.values, dti_utc.values))
dti = DatetimeIndex(start='11/6/2011 1:59',
end='11/6/2011 2:00', freq='L')
self.assertRaises(pytz.AmbiguousTimeError, dti.tz_localize,
'US/Eastern')
dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
freq='L')
self.assertRaises(
pytz.NonExistentTimeError, dti.tz_localize, 'US/Eastern')
示例9: test_with_tz_ambiguous_times
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_with_tz_ambiguous_times(self):
tz = pytz.timezone('US/Eastern')
rng = bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
freq=datetools.Hour())
self.assertRaises(pytz.NonExistentTimeError, dr.tz_localize, tz)
# after dst transition, it works
dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
freq=datetools.Hour(), tz=tz)
# November 6, 2011, fall back, repeat 2 AM hour
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
freq=datetools.Hour())
self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz)
# UTC is OK
dr = date_range(datetime(2011, 3, 13), periods=48,
freq=datetools.Minute(30), tz=pytz.utc)
示例10: test_localize_utc_conversion
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_localize_utc_conversion(self):
# Localizing to time zone should:
# 1) check for DST ambiguities
# 2) convert to UTC
rng = date_range('3/10/2012', '3/11/2012', freq='30T')
converted = rng.tz_localize(self.tzstr('US/Eastern'))
expected_naive = rng + offsets.Hour(5)
tm.assert_numpy_array_equal(converted.asi8, expected_naive.asi8)
# DST ambiguity, this should fail
rng = date_range('3/11/2012', '3/12/2012', freq='30T')
# Is this really how it should fail??
pytest.raises(NonExistentTimeError, rng.tz_localize,
self.tzstr('US/Eastern'))
示例11: test_tz_localize_dti
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_tz_localize_dti(self):
dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
freq='L')
dti2 = dti.tz_localize(self.tzstr('US/Eastern'))
dti_utc = DatetimeIndex(start='1/1/2005 05:00',
end='1/1/2005 5:00:30.256', freq='L', tz='utc')
tm.assert_numpy_array_equal(dti2.values, dti_utc.values)
dti3 = dti2.tz_convert(self.tzstr('US/Pacific'))
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
dti = DatetimeIndex(start='11/6/2011 1:59', end='11/6/2011 2:00',
freq='L')
pytest.raises(pytz.AmbiguousTimeError, dti.tz_localize,
self.tzstr('US/Eastern'))
dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
freq='L')
pytest.raises(pytz.NonExistentTimeError, dti.tz_localize,
self.tzstr('US/Eastern'))
示例12: test_with_tz_ambiguous_times
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_with_tz_ambiguous_times(self):
tz = self.tz('US/Eastern')
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
freq=offsets.Hour())
pytest.raises(pytz.NonExistentTimeError, dr.tz_localize, tz)
# after dst transition, it works
dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
freq=offsets.Hour(), tz=tz)
# November 6, 2011, fall back, repeat 2 AM hour
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
freq=offsets.Hour())
pytest.raises(pytz.AmbiguousTimeError, dr.tz_localize, tz)
# UTC is OK
dr = date_range(datetime(2011, 3, 13), periods=48,
freq=offsets.Minute(30), tz=pytz.utc)
示例13: time_zone
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def time_zone(self):
import pytz
from datetime import timedelta
tz = self.timezone
if tz is None:
return 0
now = datetime.now()
try:
offset = pytz.timezone(tz).utcoffset(now)
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
# If you're really unluckly, this offset results in a time that
# doesn't actually exist because it's within the hour that gets
# skipped when you enter DST.
offset = pytz.timezone(tz).utcoffset(now + timedelta(hours=1))
return offset.seconds / 3600
# PYBBM fields
示例14: test_dti_tz_localize_nonexistent
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_dti_tz_localize_nonexistent(self, tz, method, exp):
# GH 8917
n = 60
dti = date_range(start='2015-03-29 02:00:00', periods=n, freq='min')
if method == 'raise':
with pytest.raises(pytz.NonExistentTimeError):
dti.tz_localize(tz, nonexistent=method)
elif exp == 'invalid':
with pytest.raises(ValueError):
dti.tz_localize(tz, nonexistent=method)
else:
result = dti.tz_localize(tz, nonexistent=method)
expected = DatetimeIndex([exp] * n, tz=tz)
tm.assert_index_equal(result, expected)
示例15: test_tz_localize_nonexistent
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import NonExistentTimeError [as 别名]
def test_tz_localize_nonexistent(self, stamp, tz):
# GH#13057
ts = Timestamp(stamp)
with pytest.raises(NonExistentTimeError):
ts.tz_localize(tz)
# GH 22644
with pytest.raises(NonExistentTimeError):
with tm.assert_produces_warning(FutureWarning):
ts.tz_localize(tz, errors='raise')
with tm.assert_produces_warning(FutureWarning):
assert ts.tz_localize(tz, errors='coerce') is NaT