Skip to content

Commit 63d96f5

Browse files
jbrockmendeljreback
authored andcommitted
Organize, Split, Parametrize timezones/timestamps tests (#19473)
1 parent 3597de0 commit 63d96f5

File tree

8 files changed

+710
-626
lines changed

8 files changed

+710
-626
lines changed

pandas/tests/scalar/test_timestamp.py

+27-543
Large diffs are not rendered by default.

pandas/tests/scalar/timestamp/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
from datetime import datetime, timedelta
3+
4+
import pytest
5+
import numpy as np
6+
7+
from pandas.compat import long
8+
from pandas.tseries import offsets
9+
from pandas import Timestamp, Timedelta
10+
11+
12+
class TestTimestampArithmetic(object):
13+
def test_overflow_offset(self):
14+
# xref https://github.com/statsmodels/statsmodels/issues/3374
15+
# ends up multiplying really large numbers which overflow
16+
17+
stamp = Timestamp('2017-01-13 00:00:00', freq='D')
18+
offset = 20169940 * offsets.Day(1)
19+
20+
with pytest.raises(OverflowError):
21+
stamp + offset
22+
23+
with pytest.raises(OverflowError):
24+
offset + stamp
25+
26+
with pytest.raises(OverflowError):
27+
stamp - offset
28+
29+
def test_delta_preserve_nanos(self):
30+
val = Timestamp(long(1337299200000000123))
31+
result = val + timedelta(1)
32+
assert result.nanosecond == val.nanosecond
33+
34+
def test_timestamp_sub_datetime(self):
35+
dt = datetime(2013, 10, 12)
36+
ts = Timestamp(datetime(2013, 10, 13))
37+
assert (ts - dt).days == 1
38+
assert (dt - ts).days == -1
39+
40+
def test_addition_subtraction_types(self):
41+
# Assert on the types resulting from Timestamp +/- various date/time
42+
# objects
43+
dt = datetime(2014, 3, 4)
44+
td = timedelta(seconds=1)
45+
# build a timestamp with a frequency, since then it supports
46+
# addition/subtraction of integers
47+
ts = Timestamp(dt, freq='D')
48+
49+
assert type(ts + 1) == Timestamp
50+
assert type(ts - 1) == Timestamp
51+
52+
# Timestamp + datetime not supported, though subtraction is supported
53+
# and yields timedelta more tests in tseries/base/tests/test_base.py
54+
assert type(ts - dt) == Timedelta
55+
assert type(ts + td) == Timestamp
56+
assert type(ts - td) == Timestamp
57+
58+
# Timestamp +/- datetime64 not supported, so not tested (could possibly
59+
# assert error raised?)
60+
td64 = np.timedelta64(1, 'D')
61+
assert type(ts + td64) == Timestamp
62+
assert type(ts - td64) == Timestamp
63+
64+
def test_addition_subtraction_preserve_frequency(self):
65+
ts = Timestamp('2014-03-05', freq='D')
66+
td = timedelta(days=1)
67+
original_freq = ts.freq
68+
69+
assert (ts + 1).freq == original_freq
70+
assert (ts - 1).freq == original_freq
71+
assert (ts + td).freq == original_freq
72+
assert (ts - td).freq == original_freq
73+
74+
td64 = np.timedelta64(1, 'D')
75+
assert (ts + td64).freq == original_freq
76+
assert (ts - td64).freq == original_freq
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# -*- coding: utf-8 -*-
2+
import sys
3+
from datetime import datetime
4+
import operator
5+
6+
import pytest
7+
import numpy as np
8+
9+
from dateutil.tz import tzutc
10+
from pytz import utc
11+
12+
from pandas.compat import long
13+
from pandas import Timestamp
14+
15+
16+
class TestTimestampComparison(object):
17+
def test_comparison_object_array(self):
18+
# GH#15183
19+
ts = Timestamp('2011-01-03 00:00:00-0500', tz='US/Eastern')
20+
other = Timestamp('2011-01-01 00:00:00-0500', tz='US/Eastern')
21+
naive = Timestamp('2011-01-01 00:00:00')
22+
23+
arr = np.array([other, ts], dtype=object)
24+
res = arr == ts
25+
expected = np.array([False, True], dtype=bool)
26+
assert (res == expected).all()
27+
28+
# 2D case
29+
arr = np.array([[other, ts],
30+
[ts, other]],
31+
dtype=object)
32+
res = arr != ts
33+
expected = np.array([[True, False], [False, True]], dtype=bool)
34+
assert res.shape == expected.shape
35+
assert (res == expected).all()
36+
37+
# tzaware mismatch
38+
arr = np.array([naive], dtype=object)
39+
with pytest.raises(TypeError):
40+
arr < ts
41+
42+
def test_comparison(self):
43+
# 5-18-2012 00:00:00.000
44+
stamp = long(1337299200000000000)
45+
46+
val = Timestamp(stamp)
47+
48+
assert val == val
49+
assert not val != val
50+
assert not val < val
51+
assert val <= val
52+
assert not val > val
53+
assert val >= val
54+
55+
other = datetime(2012, 5, 18)
56+
assert val == other
57+
assert not val != other
58+
assert not val < other
59+
assert val <= other
60+
assert not val > other
61+
assert val >= other
62+
63+
other = Timestamp(stamp + 100)
64+
65+
assert val != other
66+
assert val != other
67+
assert val < other
68+
assert val <= other
69+
assert other > val
70+
assert other >= val
71+
72+
def test_compare_invalid(self):
73+
# GH 8058
74+
val = Timestamp('20130101 12:01:02')
75+
assert not val == 'foo'
76+
assert not val == 10.0
77+
assert not val == 1
78+
assert not val == long(1)
79+
assert not val == []
80+
assert not val == {'foo': 1}
81+
assert not val == np.float64(1)
82+
assert not val == np.int64(1)
83+
84+
assert val != 'foo'
85+
assert val != 10.0
86+
assert val != 1
87+
assert val != long(1)
88+
assert val != []
89+
assert val != {'foo': 1}
90+
assert val != np.float64(1)
91+
assert val != np.int64(1)
92+
93+
def test_cant_compare_tz_naive_w_aware(self):
94+
# see gh-1404
95+
a = Timestamp('3/12/2012')
96+
b = Timestamp('3/12/2012', tz='utc')
97+
98+
pytest.raises(Exception, a.__eq__, b)
99+
pytest.raises(Exception, a.__ne__, b)
100+
pytest.raises(Exception, a.__lt__, b)
101+
pytest.raises(Exception, a.__gt__, b)
102+
pytest.raises(Exception, b.__eq__, a)
103+
pytest.raises(Exception, b.__ne__, a)
104+
pytest.raises(Exception, b.__lt__, a)
105+
pytest.raises(Exception, b.__gt__, a)
106+
107+
if sys.version_info < (3, 3):
108+
pytest.raises(Exception, a.__eq__, b.to_pydatetime())
109+
pytest.raises(Exception, a.to_pydatetime().__eq__, b)
110+
else:
111+
assert not a == b.to_pydatetime()
112+
assert not a.to_pydatetime() == b
113+
114+
def test_cant_compare_tz_naive_w_aware_explicit_pytz(self):
115+
# see gh-1404
116+
a = Timestamp('3/12/2012')
117+
b = Timestamp('3/12/2012', tz=utc)
118+
119+
pytest.raises(Exception, a.__eq__, b)
120+
pytest.raises(Exception, a.__ne__, b)
121+
pytest.raises(Exception, a.__lt__, b)
122+
pytest.raises(Exception, a.__gt__, b)
123+
pytest.raises(Exception, b.__eq__, a)
124+
pytest.raises(Exception, b.__ne__, a)
125+
pytest.raises(Exception, b.__lt__, a)
126+
pytest.raises(Exception, b.__gt__, a)
127+
128+
if sys.version_info < (3, 3):
129+
pytest.raises(Exception, a.__eq__, b.to_pydatetime())
130+
pytest.raises(Exception, a.to_pydatetime().__eq__, b)
131+
else:
132+
assert not a == b.to_pydatetime()
133+
assert not a.to_pydatetime() == b
134+
135+
def test_cant_compare_tz_naive_w_aware_dateutil(self):
136+
# see gh-1404
137+
a = Timestamp('3/12/2012')
138+
b = Timestamp('3/12/2012', tz=tzutc())
139+
140+
pytest.raises(Exception, a.__eq__, b)
141+
pytest.raises(Exception, a.__ne__, b)
142+
pytest.raises(Exception, a.__lt__, b)
143+
pytest.raises(Exception, a.__gt__, b)
144+
pytest.raises(Exception, b.__eq__, a)
145+
pytest.raises(Exception, b.__ne__, a)
146+
pytest.raises(Exception, b.__lt__, a)
147+
pytest.raises(Exception, b.__gt__, a)
148+
149+
if sys.version_info < (3, 3):
150+
pytest.raises(Exception, a.__eq__, b.to_pydatetime())
151+
pytest.raises(Exception, a.to_pydatetime().__eq__, b)
152+
else:
153+
assert not a == b.to_pydatetime()
154+
assert not a.to_pydatetime() == b
155+
156+
def test_timestamp_compare_scalars(self):
157+
# case where ndim == 0
158+
lhs = np.datetime64(datetime(2013, 12, 6))
159+
rhs = Timestamp('now')
160+
nat = Timestamp('nat')
161+
162+
ops = {'gt': 'lt',
163+
'lt': 'gt',
164+
'ge': 'le',
165+
'le': 'ge',
166+
'eq': 'eq',
167+
'ne': 'ne'}
168+
169+
for left, right in ops.items():
170+
left_f = getattr(operator, left)
171+
right_f = getattr(operator, right)
172+
expected = left_f(lhs, rhs)
173+
174+
result = right_f(rhs, lhs)
175+
assert result == expected
176+
177+
expected = left_f(rhs, nat)
178+
result = right_f(nat, rhs)
179+
assert result == expected
180+
181+
def test_timestamp_compare_with_early_datetime(self):
182+
# e.g. datetime.min
183+
stamp = Timestamp('2012-01-01')
184+
185+
assert not stamp == datetime.min
186+
assert not stamp == datetime(1600, 1, 1)
187+
assert not stamp == datetime(2700, 1, 1)
188+
assert stamp != datetime.min
189+
assert stamp != datetime(1600, 1, 1)
190+
assert stamp != datetime(2700, 1, 1)
191+
assert stamp > datetime(1600, 1, 1)
192+
assert stamp >= datetime(1600, 1, 1)
193+
assert stamp < datetime(2700, 1, 1)
194+
assert stamp <= datetime(2700, 1, 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
import dateutil
5+
import pytz # noqa # a test below uses pytz but only inside a `eval` call
6+
7+
import pprint
8+
from distutils.version import LooseVersion
9+
10+
from pandas import Timestamp
11+
12+
13+
class TestTimestampRendering(object):
14+
15+
# dateutil zone change (only matters for repr)
16+
if LooseVersion(dateutil.__version__) >= LooseVersion('2.6.0'):
17+
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern',
18+
'dateutil/US/Pacific']
19+
else:
20+
timezones = ['UTC', 'Asia/Tokyo', 'US/Eastern',
21+
'dateutil/America/Los_Angeles']
22+
23+
@pytest.mark.parametrize('tz', timezones)
24+
@pytest.mark.parametrize('freq', ['D', 'M', 'S', 'N'])
25+
@pytest.mark.parametrize('date', ['2014-03-07', '2014-01-01 09:00',
26+
'2014-01-01 00:00:00.000000001'])
27+
def test_repr(self, date, freq, tz):
28+
# avoid to match with timezone name
29+
freq_repr = "'{0}'".format(freq)
30+
if tz.startswith('dateutil'):
31+
tz_repr = tz.replace('dateutil', '')
32+
else:
33+
tz_repr = tz
34+
35+
date_only = Timestamp(date)
36+
assert date in repr(date_only)
37+
assert tz_repr not in repr(date_only)
38+
assert freq_repr not in repr(date_only)
39+
assert date_only == eval(repr(date_only))
40+
41+
date_tz = Timestamp(date, tz=tz)
42+
assert date in repr(date_tz)
43+
assert tz_repr in repr(date_tz)
44+
assert freq_repr not in repr(date_tz)
45+
assert date_tz == eval(repr(date_tz))
46+
47+
date_freq = Timestamp(date, freq=freq)
48+
assert date in repr(date_freq)
49+
assert tz_repr not in repr(date_freq)
50+
assert freq_repr in repr(date_freq)
51+
assert date_freq == eval(repr(date_freq))
52+
53+
date_tz_freq = Timestamp(date, tz=tz, freq=freq)
54+
assert date in repr(date_tz_freq)
55+
assert tz_repr in repr(date_tz_freq)
56+
assert freq_repr in repr(date_tz_freq)
57+
assert date_tz_freq == eval(repr(date_tz_freq))
58+
59+
def test_repr_utcoffset(self):
60+
# This can cause the tz field to be populated, but it's redundant to
61+
# include this information in the date-string.
62+
date_with_utc_offset = Timestamp('2014-03-13 00:00:00-0400', tz=None)
63+
assert '2014-03-13 00:00:00-0400' in repr(date_with_utc_offset)
64+
assert 'tzoffset' not in repr(date_with_utc_offset)
65+
assert 'pytz.FixedOffset(-240)' in repr(date_with_utc_offset)
66+
expr = repr(date_with_utc_offset).replace("'pytz.FixedOffset(-240)'",
67+
'pytz.FixedOffset(-240)')
68+
assert date_with_utc_offset == eval(expr)
69+
70+
def test_timestamp_repr_pre1900(self):
71+
# pre-1900
72+
stamp = Timestamp('1850-01-01', tz='US/Eastern')
73+
repr(stamp)
74+
75+
iso8601 = '1850-01-01 01:23:45.012345'
76+
stamp = Timestamp(iso8601, tz='US/Eastern')
77+
result = repr(stamp)
78+
assert iso8601 in result
79+
80+
def test_pprint(self):
81+
# GH#12622
82+
nested_obj = {'foo': 1,
83+
'bar': [{'w': {'a': Timestamp('2011-01-01')}}] * 10}
84+
result = pprint.pformat(nested_obj, width=50)
85+
expected = r"""{'bar': [{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
86+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
87+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
88+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
89+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
90+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
91+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
92+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
93+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
94+
{'w': {'a': Timestamp('2011-01-01 00:00:00')}}],
95+
'foo': 1}"""
96+
assert result == expected

0 commit comments

Comments
 (0)