forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unary_ops.py
282 lines (229 loc) · 10 KB
/
test_unary_ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -*- coding: utf-8 -*-
from datetime import datetime
import pytest
import pytz
from pytz import utc
from dateutil.tz import gettz
import pandas.util.testing as tm
import pandas.util._test_decorators as td
from pandas.compat import PY3
from pandas._libs import tslib
from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR
from pandas import Timestamp, NaT
class TestTimestampUnaryOps(object):
# --------------------------------------------------------------
# Timestamp.round
def test_round_day_naive(self):
dt = Timestamp('20130101 09:10:11')
result = dt.round('D')
expected = Timestamp('20130101')
assert result == expected
dt = Timestamp('20130101 19:10:11')
result = dt.round('D')
expected = Timestamp('20130102')
assert result == expected
dt = Timestamp('20130201 12:00:00')
result = dt.round('D')
expected = Timestamp('20130202')
assert result == expected
dt = Timestamp('20130104 12:00:00')
result = dt.round('D')
expected = Timestamp('20130105')
assert result == expected
def test_round_tzaware(self):
dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
result = dt.round('D')
expected = Timestamp('20130101', tz='US/Eastern')
assert result == expected
dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
result = dt.round('s')
assert result == dt
def test_round_30min(self):
# round
dt = Timestamp('20130104 12:32:00')
result = dt.round('30Min')
expected = Timestamp('20130104 12:30:00')
assert result == expected
def test_round_subsecond(self):
# GH#14440 & GH#15578
result = Timestamp('2016-10-17 12:00:00.0015').round('ms')
expected = Timestamp('2016-10-17 12:00:00.002000')
assert result == expected
result = Timestamp('2016-10-17 12:00:00.00149').round('ms')
expected = Timestamp('2016-10-17 12:00:00.001000')
assert result == expected
ts = Timestamp('2016-10-17 12:00:00.0015')
for freq in ['us', 'ns']:
assert ts == ts.round(freq)
result = Timestamp('2016-10-17 12:00:00.001501031').round('10ns')
expected = Timestamp('2016-10-17 12:00:00.001501030')
assert result == expected
def test_round_nonstandard_freq(self):
with tm.assert_produces_warning():
Timestamp('2016-10-17 12:00:00.001501031').round('1010ns')
def test_round_invalid_arg(self):
stamp = Timestamp('2000-01-05 05:09:15.13')
with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR):
stamp.round('foo')
@pytest.mark.parametrize('freq, expected', [
('D', Timestamp('2000-01-05 00:00:00')),
('H', Timestamp('2000-01-05 05:00:00')),
('S', Timestamp('2000-01-05 05:09:15'))])
def test_round_frequencies(self, freq, expected):
stamp = Timestamp('2000-01-05 05:09:15.13')
result = stamp.round(freq=freq)
assert result == expected
@pytest.mark.parametrize('test_input, rounder, freq, expected', [
('2117-01-01 00:00:45', 'floor', '15s', '2117-01-01 00:00:45'),
('2117-01-01 00:00:45', 'ceil', '15s', '2117-01-01 00:00:45'),
('2117-01-01 00:00:45.000000012', 'floor', '10ns',
'2117-01-01 00:00:45.000000010'),
('1823-01-01 00:00:01.000000012', 'ceil', '10ns',
'1823-01-01 00:00:01.000000020'),
('1823-01-01 00:00:01', 'floor', '1s', '1823-01-01 00:00:01'),
('1823-01-01 00:00:01', 'ceil', '1s', '1823-01-01 00:00:01'),
('NaT', 'floor', '1s', 'NaT'),
('NaT', 'ceil', '1s', 'NaT')
])
def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
dt = Timestamp(test_input)
func = getattr(dt, rounder)
result = func(freq)
if dt is NaT:
assert result is NaT
else:
expected = Timestamp(expected)
assert result == expected
@pytest.mark.parametrize('test_input, freq, expected', [
('2018-01-01 00:02:00', '2T', '2018-01-01 00:02:00'),
('2018-01-01 00:04:00', '4T', '2018-01-01 00:04:00'),
('2018-01-01 00:15:00', '15T', '2018-01-01 00:15:00'),
('2018-01-01 00:20:00', '20T', '2018-01-01 00:20:00'),
])
def test_round_minute_freq(self, test_input, freq, expected):
# ensure timestamps that shouldn't round don't
# GH#21262
dt = Timestamp(test_input)
expected = Timestamp(expected)
result_ceil = dt.ceil(freq)
assert result_ceil == expected
result_floor = dt.floor(freq)
assert result_floor == expected
result_round = dt.round(freq)
assert result_round == expected
def test_ceil(self):
dt = Timestamp('20130101 09:10:11')
result = dt.ceil('D')
expected = Timestamp('20130102')
assert result == expected
def test_floor(self):
dt = Timestamp('20130101 09:10:11')
result = dt.floor('D')
expected = Timestamp('20130101')
assert result == expected
# --------------------------------------------------------------
# Timestamp.replace
def test_replace_naive(self):
# GH#14621, GH#7825
ts = Timestamp('2016-01-01 09:00:00')
result = ts.replace(hour=0)
expected = Timestamp('2016-01-01 00:00:00')
assert result == expected
def test_replace_aware(self, tz_aware_fixture):
tz = tz_aware_fixture
# GH#14621, GH#7825
# replacing datetime components with and w/o presence of a timezone
ts = Timestamp('2016-01-01 09:00:00', tz=tz)
result = ts.replace(hour=0)
expected = Timestamp('2016-01-01 00:00:00', tz=tz)
assert result == expected
def test_replace_preserves_nanos(self, tz_aware_fixture):
tz = tz_aware_fixture
# GH#14621, GH#7825
ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
result = ts.replace(hour=0)
expected = Timestamp('2016-01-01 00:00:00.000000123', tz=tz)
assert result == expected
def test_replace_multiple(self, tz_aware_fixture):
tz = tz_aware_fixture
# GH#14621, GH#7825
# replacing datetime components with and w/o presence of a timezone
# test all
ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
result = ts.replace(year=2015, month=2, day=2, hour=0, minute=5,
second=5, microsecond=5, nanosecond=5)
expected = Timestamp('2015-02-02 00:05:05.000005005', tz=tz)
assert result == expected
def test_replace_invalid_kwarg(self, tz_aware_fixture):
tz = tz_aware_fixture
# GH#14621, GH#7825
ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
with pytest.raises(TypeError):
ts.replace(foo=5)
def test_replace_integer_args(self, tz_aware_fixture):
tz = tz_aware_fixture
# GH#14621, GH#7825
ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
with pytest.raises(ValueError):
ts.replace(hour=0.1)
def test_replace_tzinfo_equiv_tz_localize_none(self):
# GH#14621, GH#7825
# assert conversion to naive is the same as replacing tzinfo with None
ts = Timestamp('2013-11-03 01:59:59.999999-0400', tz='US/Eastern')
assert ts.tz_localize(None) == ts.replace(tzinfo=None)
@td.skip_if_windows
def test_replace_tzinfo(self):
# GH#15683
dt = datetime(2016, 3, 27, 1)
tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo
result_dt = dt.replace(tzinfo=tzinfo)
result_pd = Timestamp(dt).replace(tzinfo=tzinfo)
if PY3:
# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):
assert result_dt.timestamp() == result_pd.timestamp()
assert result_dt == result_pd
assert result_dt == result_pd.to_pydatetime()
result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None)
result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None)
if PY3:
# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):
assert result_dt.timestamp() == result_pd.timestamp()
assert result_dt == result_pd
assert result_dt == result_pd.to_pydatetime()
@pytest.mark.parametrize('tz, normalize', [
(pytz.timezone('US/Eastern'), lambda x: x.tzinfo.normalize(x)),
(gettz('US/Eastern'), lambda x: x)])
def test_replace_across_dst(self, tz, normalize):
# GH#18319 check that 1) timezone is correctly normalized and
# 2) that hour is not incorrectly changed by this normalization
ts_naive = Timestamp('2017-12-03 16:03:30')
ts_aware = tslib._localize_pydatetime(ts_naive, tz)
# Preliminary sanity-check
assert ts_aware == normalize(ts_aware)
# Replace across DST boundary
ts2 = ts_aware.replace(month=6)
# Check that `replace` preserves hour literal
assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute)
# Check that post-replace object is appropriately normalized
ts2b = normalize(ts2)
assert ts2 == ts2b
# --------------------------------------------------------------
@td.skip_if_windows
def test_timestamp(self):
# GH#17329
# tz-naive --> treat it as if it were UTC for purposes of timestamp()
ts = Timestamp.now()
uts = ts.replace(tzinfo=utc)
assert ts.timestamp() == uts.timestamp()
tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
utsc = tsc.tz_convert('UTC')
# utsc is a different representation of the same time
assert tsc.timestamp() == utsc.timestamp()
if PY3:
# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):
# should agree with datetime.timestamp method
dt = ts.to_pydatetime()
assert dt.timestamp() == ts.timestamp()