Skip to content

Commit 5a20717

Browse files
jbrockmendeljreback
authored andcommitted
tests for tslibs.conversion and tslibs.timezones (#19642)
1 parent 39e7b69 commit 5a20717

File tree

3 files changed

+95
-85
lines changed

3 files changed

+95
-85
lines changed

pandas/tests/tseries/test_timezones.py

+1-85
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
import pytest
33

44
import pytz
5-
import dateutil
6-
import numpy as np
75

86
from datetime import datetime
97

10-
import pandas.util.testing as tm
11-
from pandas.core.indexes.datetimes import date_range
12-
from pandas._libs import tslib
13-
from pandas._libs.tslibs import timezones, conversion
8+
from pandas._libs.tslibs import timezones
149
from pandas import Timestamp
1510

1611

@@ -111,82 +106,3 @@ def localize(self, tz, x):
111106
def normalize(self, ts):
112107
# no-op for dateutil
113108
return ts
114-
115-
def test_tzlocal(self):
116-
# GH 13583
117-
ts = Timestamp('2011-01-01', tz=dateutil.tz.tzlocal())
118-
assert ts.tz == dateutil.tz.tzlocal()
119-
assert "tz='tzlocal()')" in repr(ts)
120-
121-
tz = timezones.maybe_get_tz('tzlocal()')
122-
assert tz == dateutil.tz.tzlocal()
123-
124-
# get offset using normal datetime for test
125-
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
126-
offset = offset.total_seconds() * 1000000000
127-
assert ts.value + offset == Timestamp('2011-01-01').value
128-
129-
130-
class TestTimeZoneCacheKey(object):
131-
132-
@pytest.mark.parametrize('tz_name', list(pytz.common_timezones))
133-
def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self, tz_name):
134-
if tz_name == 'UTC':
135-
# skip utc as it's a special case in dateutil
136-
return
137-
tz_p = timezones.maybe_get_tz(tz_name)
138-
tz_d = timezones.maybe_get_tz('dateutil/' + tz_name)
139-
if tz_d is None:
140-
# skip timezones that dateutil doesn't know about.
141-
return
142-
assert (timezones._p_tz_cache_key(tz_p) !=
143-
timezones._p_tz_cache_key(tz_d))
144-
145-
146-
class TestTslib(object):
147-
148-
def test_tslib_tz_convert(self):
149-
def compare_utc_to_local(tz_didx, utc_didx):
150-
f = lambda x: conversion.tz_convert_single(x, 'UTC', tz_didx.tz)
151-
result = conversion.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz)
152-
result_single = np.vectorize(f)(tz_didx.asi8)
153-
tm.assert_numpy_array_equal(result, result_single)
154-
155-
def compare_local_to_utc(tz_didx, utc_didx):
156-
f = lambda x: conversion.tz_convert_single(x, tz_didx.tz, 'UTC')
157-
result = conversion.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC')
158-
result_single = np.vectorize(f)(utc_didx.asi8)
159-
tm.assert_numpy_array_equal(result, result_single)
160-
161-
for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']:
162-
# US: 2014-03-09 - 2014-11-11
163-
# MOSCOW: 2014-10-26 / 2014-12-31
164-
tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz)
165-
utc_didx = date_range('2014-03-01', '2015-01-10', freq='H')
166-
compare_utc_to_local(tz_didx, utc_didx)
167-
# local tz to UTC can be differ in hourly (or higher) freqs because
168-
# of DST
169-
compare_local_to_utc(tz_didx, utc_didx)
170-
171-
tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz)
172-
utc_didx = date_range('2000-01-01', '2020-01-01', freq='D')
173-
compare_utc_to_local(tz_didx, utc_didx)
174-
compare_local_to_utc(tz_didx, utc_didx)
175-
176-
tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz)
177-
utc_didx = date_range('2000-01-01', '2100-01-01', freq='A')
178-
compare_utc_to_local(tz_didx, utc_didx)
179-
compare_local_to_utc(tz_didx, utc_didx)
180-
181-
# Check empty array
182-
result = conversion.tz_convert(np.array([], dtype=np.int64),
183-
timezones.maybe_get_tz('US/Eastern'),
184-
timezones.maybe_get_tz('Asia/Tokyo'))
185-
tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64))
186-
187-
# Check all-NaT array
188-
result = conversion.tz_convert(np.array([tslib.iNaT], dtype=np.int64),
189-
timezones.maybe_get_tz('US/Eastern'),
190-
timezones.maybe_get_tz('Asia/Tokyo'))
191-
tm.assert_numpy_array_equal(result, np.array(
192-
[tslib.iNaT], dtype=np.int64))
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas.util.testing as tm
7+
from pandas import date_range
8+
from pandas._libs.tslib import iNaT
9+
from pandas._libs.tslibs import conversion, timezones
10+
11+
12+
def compare_utc_to_local(tz_didx, utc_didx):
13+
f = lambda x: conversion.tz_convert_single(x, 'UTC', tz_didx.tz)
14+
result = conversion.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz)
15+
result_single = np.vectorize(f)(tz_didx.asi8)
16+
tm.assert_numpy_array_equal(result, result_single)
17+
18+
19+
def compare_local_to_utc(tz_didx, utc_didx):
20+
f = lambda x: conversion.tz_convert_single(x, tz_didx.tz, 'UTC')
21+
result = conversion.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC')
22+
result_single = np.vectorize(f)(utc_didx.asi8)
23+
tm.assert_numpy_array_equal(result, result_single)
24+
25+
26+
class TestTZConvert(object):
27+
28+
@pytest.mark.parametrize('tz', ['UTC', 'Asia/Tokyo',
29+
'US/Eastern', 'Europe/Moscow'])
30+
def test_tz_convert_single_matches_tz_convert_hourly(self, tz):
31+
# US: 2014-03-09 - 2014-11-11
32+
# MOSCOW: 2014-10-26 / 2014-12-31
33+
tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz)
34+
utc_didx = date_range('2014-03-01', '2015-01-10', freq='H')
35+
compare_utc_to_local(tz_didx, utc_didx)
36+
37+
# local tz to UTC can be differ in hourly (or higher) freqs because
38+
# of DST
39+
compare_local_to_utc(tz_didx, utc_didx)
40+
41+
@pytest.mark.parametrize('tz', ['UTC', 'Asia/Tokyo',
42+
'US/Eastern', 'Europe/Moscow'])
43+
@pytest.mark.parametrize('freq', ['D', 'A'])
44+
def test_tz_convert_single_matches_tz_convert(self, tz, freq):
45+
tz_didx = date_range('2000-01-01', '2020-01-01', freq=freq, tz=tz)
46+
utc_didx = date_range('2000-01-01', '2020-01-01', freq=freq)
47+
compare_utc_to_local(tz_didx, utc_didx)
48+
compare_local_to_utc(tz_didx, utc_didx)
49+
50+
@pytest.mark.parametrize('arr', [
51+
pytest.param(np.array([], dtype=np.int64), id='empty'),
52+
pytest.param(np.array([iNaT], dtype=np.int64), id='all_nat')])
53+
def test_tz_convert_corner(self, arr):
54+
result = conversion.tz_convert(arr,
55+
timezones.maybe_get_tz('US/Eastern'),
56+
timezones.maybe_get_tz('Asia/Tokyo'))
57+
tm.assert_numpy_array_equal(result, arr)

pandas/tests/tslibs/test_timezones.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
from datetime import datetime
3+
4+
import pytest
5+
import pytz
6+
import dateutil.tz
7+
8+
from pandas._libs.tslibs import timezones
9+
from pandas import Timestamp
10+
11+
12+
@pytest.mark.parametrize('tz_name', list(pytz.common_timezones))
13+
def test_cache_keys_are_distinct_for_pytz_vs_dateutil(tz_name):
14+
if tz_name == 'UTC':
15+
# skip utc as it's a special case in dateutil
16+
return
17+
tz_p = timezones.maybe_get_tz(tz_name)
18+
tz_d = timezones.maybe_get_tz('dateutil/' + tz_name)
19+
if tz_d is None:
20+
# skip timezones that dateutil doesn't know about.
21+
return
22+
assert timezones._p_tz_cache_key(tz_p) != timezones._p_tz_cache_key(tz_d)
23+
24+
25+
def test_tzlocal():
26+
# GH#13583
27+
ts = Timestamp('2011-01-01', tz=dateutil.tz.tzlocal())
28+
assert ts.tz == dateutil.tz.tzlocal()
29+
assert "tz='tzlocal()')" in repr(ts)
30+
31+
tz = timezones.maybe_get_tz('tzlocal()')
32+
assert tz == dateutil.tz.tzlocal()
33+
34+
# get offset using normal datetime for test
35+
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
36+
offset = offset.total_seconds() * 1000000000
37+
assert ts.value + offset == Timestamp('2011-01-01').value

0 commit comments

Comments
 (0)