-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy pathtest_constructors.py
388 lines (333 loc) · 12.4 KB
/
test_constructors.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from datetime import timedelta
from itertools import product
import numpy as np
import pytest
from pandas._libs.tslibs import OutOfBoundsTimedelta
from pandas import (
Timedelta,
offsets,
to_timedelta,
)
def test_construct_from_td64_with_unit(td_overflow_msg: str):
# ignore the unit, as it may cause silently overflows leading to incorrect
# results, and in non-overflow cases is irrelevant GH#46827
obj = np.timedelta64(123456789, "h")
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(obj, unit="ns")
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(obj)
def test_construction():
expected = np.timedelta64(10, "D").astype("m8[ns]").view("i8")
assert Timedelta(10, unit="d").value == expected
assert Timedelta(10.0, unit="d").value == expected
assert Timedelta("10 days").value == expected
assert Timedelta(days=10).value == expected
assert Timedelta(days=10.0).value == expected
expected += np.timedelta64(10, "s").astype("m8[ns]").view("i8")
assert Timedelta("10 days 00:00:10").value == expected
assert Timedelta(days=10, seconds=10).value == expected
assert Timedelta(days=10, milliseconds=10 * 1000).value == expected
assert Timedelta(days=10, microseconds=10 * 1000 * 1000).value == expected
# rounding cases
assert Timedelta(82739999850000).value == 82739999850000
assert "0 days 22:58:59.999850" in str(Timedelta(82739999850000))
assert Timedelta(123072001000000).value == 123072001000000
assert "1 days 10:11:12.001" in str(Timedelta(123072001000000))
# string conversion with/without leading zero
# GH#9570
assert Timedelta("0:00:00") == timedelta(hours=0)
assert Timedelta("00:00:00") == timedelta(hours=0)
assert Timedelta("-1:00:00") == -timedelta(hours=1)
assert Timedelta("-01:00:00") == -timedelta(hours=1)
# more strings & abbrevs
# GH#8190
assert Timedelta("1 h") == timedelta(hours=1)
assert Timedelta("1 hour") == timedelta(hours=1)
assert Timedelta("1 hr") == timedelta(hours=1)
assert Timedelta("1 hours") == timedelta(hours=1)
assert Timedelta("-1 hours") == -timedelta(hours=1)
assert Timedelta("1 m") == timedelta(minutes=1)
assert Timedelta("1.5 m") == timedelta(seconds=90)
assert Timedelta("1 minute") == timedelta(minutes=1)
assert Timedelta("1 minutes") == timedelta(minutes=1)
assert Timedelta("1 s") == timedelta(seconds=1)
assert Timedelta("1 second") == timedelta(seconds=1)
assert Timedelta("1 seconds") == timedelta(seconds=1)
assert Timedelta("1 ms") == timedelta(milliseconds=1)
assert Timedelta("1 milli") == timedelta(milliseconds=1)
assert Timedelta("1 millisecond") == timedelta(milliseconds=1)
assert Timedelta("1 us") == timedelta(microseconds=1)
assert Timedelta("1 µs") == timedelta(microseconds=1)
assert Timedelta("1 micros") == timedelta(microseconds=1)
assert Timedelta("1 microsecond") == timedelta(microseconds=1)
assert Timedelta("1.5 microsecond") == Timedelta("00:00:00.000001500")
assert Timedelta("1 ns") == Timedelta("00:00:00.000000001")
assert Timedelta("1 nano") == Timedelta("00:00:00.000000001")
assert Timedelta("1 nanosecond") == Timedelta("00:00:00.000000001")
# combos
assert Timedelta("10 days 1 hour") == timedelta(days=10, hours=1)
assert Timedelta("10 days 1 h") == timedelta(days=10, hours=1)
assert Timedelta("10 days 1 h 1m 1s") == timedelta(
days=10, hours=1, minutes=1, seconds=1
)
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
days=10, hours=1, minutes=1, seconds=1
)
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
days=10, hours=1, minutes=1, seconds=1
)
assert Timedelta("-10 days 1 h 1m 1s 3us") == -timedelta(
days=10, hours=1, minutes=1, seconds=1, microseconds=3
)
assert Timedelta("-10 days 1 h 1.5m 1s 3us") == -timedelta(
days=10, hours=1, minutes=1, seconds=31, microseconds=3
)
# Currently invalid as it has a - on the hh:mm:dd part
# (only allowed on the days)
msg = "only leading negative signs are allowed"
with pytest.raises(ValueError, match=msg):
Timedelta("-10 days -1 h 1.5m 1s 3us")
# only leading neg signs are allowed
with pytest.raises(ValueError, match=msg):
Timedelta("10 days -1 h 1.5m 1s 3us")
# no units specified
msg = "no units specified"
with pytest.raises(ValueError, match=msg):
Timedelta("3.1415")
# invalid construction
msg = "cannot construct a Timedelta"
with pytest.raises(ValueError, match=msg):
Timedelta()
msg = "unit abbreviation w/o a number"
with pytest.raises(ValueError, match=msg):
Timedelta("foo")
msg = (
"cannot construct a Timedelta from "
"the passed arguments, allowed keywords are "
)
with pytest.raises(ValueError, match=msg):
Timedelta(day=10)
# floats
expected = np.timedelta64(10, "s").astype("m8[ns]").view("i8") + np.timedelta64(
500, "ms"
).astype("m8[ns]").view("i8")
assert Timedelta(10.5, unit="s").value == expected
# offset
assert to_timedelta(offsets.Hour(2)) == Timedelta(hours=2)
assert Timedelta(offsets.Hour(2)) == Timedelta(hours=2)
assert Timedelta(offsets.Second(2)) == Timedelta(seconds=2)
# GH#11995: unicode
expected = Timedelta("1H")
result = Timedelta("1H")
assert result == expected
assert to_timedelta(offsets.Hour(2)) == Timedelta("0 days, 02:00:00")
msg = "unit abbreviation w/o a number"
with pytest.raises(ValueError, match=msg):
Timedelta("foo bar")
@pytest.mark.parametrize(
"item",
list(
{
"days": "D",
"seconds": "s",
"microseconds": "us",
"milliseconds": "ms",
"minutes": "m",
"hours": "h",
"weeks": "W",
}.items()
),
)
@pytest.mark.parametrize(
"npdtype", [np.int64, np.int32, np.int16, np.float64, np.float32, np.float16]
)
def test_td_construction_with_np_dtypes(npdtype, item):
# GH#8757: test construction with np dtypes
pykwarg, npkwarg = item
expected = np.timedelta64(1, npkwarg).astype("m8[ns]").view("i8")
assert Timedelta(**{pykwarg: npdtype(1)}).value == expected
@pytest.mark.parametrize(
"val",
[
"1s",
"-1s",
"1us",
"-1us",
"1 day",
"-1 day",
"-23:59:59.999999",
"-1 days +23:59:59.999999",
"-1ns",
"1ns",
"-23:59:59.999999999",
],
)
def test_td_from_repr_roundtrip(val):
# round-trip both for string and value
td = Timedelta(val)
assert Timedelta(td.value) == td
assert Timedelta(str(td)) == td
assert Timedelta(td._repr_base(format="all")) == td
assert Timedelta(td._repr_base()) == td
def test_overflow_on_construction(td_overflow_msg: str):
# GH#3374
value = Timedelta("1day").value * 20169940
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(value)
# xref GH#17637
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(7 * 19999, unit="D")
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(timedelta(days=13 * 19999))
@pytest.mark.parametrize(
"val, unit, name",
[
(3508, "M", " months"),
(15251, "W", " weeks"), # 1
(106752, "D", " days"), # change from previous:
(2562048, "h", " hours"), # 0 hours
(153722868, "m", " minutes"), # 13 minutes
(9223372037, "s", " seconds"), # 44 seconds
],
)
def test_construction_out_of_bounds_td64(val, unit, name, td_overflow_msg: str):
# TODO: parametrize over units just above/below the implementation bounds
# once GH#38964 is resolved
# Timedelta.max is just under 106752 days
td64 = np.timedelta64(val, unit)
assert td64.astype("m8[ns]").view("i8") < 0 # i.e. naive astype will be wrong
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(td64)
# But just back in bounds and we are OK
assert Timedelta(td64 - 1) == td64 - 1
td64 *= -1
assert td64.astype("m8[ns]").view("i8") > 0 # i.e. naive astype will be wrong
with pytest.raises(OutOfBoundsTimedelta, match=td_overflow_msg):
Timedelta(td64)
# But just back in bounds and we are OK
assert Timedelta(td64 + 1) == td64 + 1
@pytest.mark.parametrize(
"fmt,exp",
[
(
"P6DT0H50M3.010010012S",
Timedelta(
days=6,
minutes=50,
seconds=3,
milliseconds=10,
microseconds=10,
nanoseconds=12,
),
),
(
"P-6DT0H50M3.010010012S",
Timedelta(
days=-6,
minutes=50,
seconds=3,
milliseconds=10,
microseconds=10,
nanoseconds=12,
),
),
("P4DT12H30M5S", Timedelta(days=4, hours=12, minutes=30, seconds=5)),
("P0DT0H0M0.000000123S", Timedelta(nanoseconds=123)),
("P0DT0H0M0.00001S", Timedelta(microseconds=10)),
("P0DT0H0M0.001S", Timedelta(milliseconds=1)),
("P0DT0H1M0S", Timedelta(minutes=1)),
("P1DT25H61M61S", Timedelta(days=1, hours=25, minutes=61, seconds=61)),
("PT1S", Timedelta(seconds=1)),
("PT0S", Timedelta(seconds=0)),
("P1WT0S", Timedelta(days=7, seconds=0)),
("P1D", Timedelta(days=1)),
("P1DT1H", Timedelta(days=1, hours=1)),
("P1W", Timedelta(days=7)),
("PT300S", Timedelta(seconds=300)),
("P1DT0H0M00000000000S", Timedelta(days=1)),
("PT-6H3M", Timedelta(hours=-6, minutes=3)),
("-PT6H3M", Timedelta(hours=-6, minutes=-3)),
("-PT-6H+3M", Timedelta(hours=6, minutes=-3)),
],
)
def test_iso_constructor(fmt, exp):
assert Timedelta(fmt) == exp
@pytest.mark.parametrize(
"fmt",
[
"PPPPPPPPPPPP",
"PDTHMS",
"P0DT999H999M999S",
"P1DT0H0M0.0000000000000S",
"P1DT0H0M0.S",
"P",
"-P",
],
)
def test_iso_constructor_raises(fmt):
msg = f"Invalid ISO 8601 Duration format - {fmt}"
with pytest.raises(ValueError, match=msg):
Timedelta(fmt)
@pytest.mark.parametrize(
"constructed_td, conversion",
[
(Timedelta(nanoseconds=100), "100ns"),
(
Timedelta(
days=1,
hours=1,
minutes=1,
weeks=1,
seconds=1,
milliseconds=1,
microseconds=1,
nanoseconds=1,
),
694861001001001,
),
(Timedelta(microseconds=1) + Timedelta(nanoseconds=1), "1us1ns"),
(Timedelta(microseconds=1) - Timedelta(nanoseconds=1), "999ns"),
(Timedelta(microseconds=1) + 5 * Timedelta(nanoseconds=-2), "990ns"),
],
)
def test_td_constructor_on_nanoseconds(constructed_td, conversion):
# GH#9273
assert constructed_td == Timedelta(conversion)
def test_td_constructor_value_error():
msg = "Invalid type <class 'str'>. Must be int or float."
with pytest.raises(TypeError, match=msg):
Timedelta(nanoseconds="abc")
def test_timedelta_constructor_identity():
# Test for #30543
expected = Timedelta(np.timedelta64(1, "s"))
result = Timedelta(expected)
assert result is expected
@pytest.mark.parametrize(
"constructor, value, unit, expectation",
[
(Timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
(to_timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
(to_timedelta, ["1", 2, 3], "s", (ValueError, "unit must not be specified")),
],
)
def test_string_with_unit(constructor, value, unit, expectation):
exp, match = expectation
with pytest.raises(exp, match=match):
_ = constructor(value, unit=unit)
@pytest.mark.parametrize(
"value",
[
"".join(elements)
for repetition in (1, 2)
for elements in product("+-, ", repeat=repetition)
],
)
def test_string_without_numbers(value):
# GH39710 Timedelta input string with only symbols and no digits raises an error
msg = (
"symbols w/o a number"
if value != "--"
else "only leading negative signs are allowed"
)
with pytest.raises(ValueError, match=msg):
Timedelta(value)