forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_timedeltas.py
146 lines (113 loc) · 4.42 KB
/
test_timedeltas.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
import re
import numpy as np
import pytest
from pandas._libs.tslibs.timedeltas import (
array_to_timedelta64,
delta_to_nanoseconds,
ints_to_pytimedelta,
)
from pandas import (
Timedelta,
offsets,
)
import pandas._testing as tm
@pytest.mark.parametrize(
"obj,expected",
[
(np.timedelta64(14, "D"), 14 * 24 * 3600 * 1e9),
(Timedelta(minutes=-7), -7 * 60 * 1e9),
(Timedelta(minutes=-7).to_pytimedelta(), -7 * 60 * 1e9),
(Timedelta(seconds=1234e-9), 1234), # GH43764, GH40946
(
Timedelta(seconds=1e-9, milliseconds=1e-5, microseconds=1e-1),
111,
), # GH43764
(
Timedelta(days=1, seconds=1e-9, milliseconds=1e-5, microseconds=1e-1),
24 * 3600e9 + 111,
), # GH43764
(offsets.Nano(125), 125),
],
)
def test_delta_to_nanoseconds(obj, expected):
result = delta_to_nanoseconds(obj)
assert result == expected
def test_delta_to_nanoseconds_error():
obj = np.array([123456789], dtype="m8[ns]")
with pytest.raises(TypeError, match="<class 'numpy.ndarray'>"):
delta_to_nanoseconds(obj)
with pytest.raises(TypeError, match="float"):
delta_to_nanoseconds(1.5)
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(1)
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(np.int64(2))
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(np.int32(3))
def test_delta_to_nanoseconds_td64_MY_raises():
msg = (
"delta_to_nanoseconds does not support Y or M units, "
"as their duration in nanoseconds is ambiguous"
)
td = np.timedelta64(1234, "Y")
with pytest.raises(ValueError, match=msg):
delta_to_nanoseconds(td)
td = np.timedelta64(1234, "M")
with pytest.raises(ValueError, match=msg):
delta_to_nanoseconds(td)
@pytest.mark.parametrize("unit", ["Y", "M"])
def test_unsupported_td64_unit_raises(unit):
# GH 52806
with pytest.raises(
ValueError, match=f"cannot construct a Timedelta from a unit {unit}"
):
Timedelta(np.timedelta64(1, unit))
def test_huge_nanoseconds_overflow():
# GH 32402
assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10
assert delta_to_nanoseconds(Timedelta(nanoseconds=1e10)) == 1e10
@pytest.mark.parametrize(
"kwargs", [{"Seconds": 1}, {"seconds": 1, "Nanoseconds": 1}, {"Foo": 2}]
)
def test_kwarg_assertion(kwargs):
err_message = (
"cannot construct a Timedelta from the passed arguments, "
"allowed keywords are "
"[weeks, days, hours, minutes, seconds, "
"milliseconds, microseconds, nanoseconds]"
)
with pytest.raises(ValueError, match=re.escape(err_message)):
Timedelta(**kwargs)
class TestArrayToTimedelta64:
def test_array_to_timedelta64_string_with_unit_2d_raises(self):
# check the 'unit is not None and errors != "coerce"' path
# in array_to_timedelta64 raises correctly with 2D values
values = np.array([["1", 2], [3, "4"]], dtype=object)
with pytest.raises(ValueError, match="unit must not be specified"):
array_to_timedelta64(values, unit="s")
def test_array_to_timedelta64_non_object_raises(self):
# check we raise, not segfault
values = np.arange(5)
msg = "'values' must have object dtype"
with pytest.raises(TypeError, match=msg):
array_to_timedelta64(values)
@pytest.mark.parametrize("unit", ["s", "ms", "us"])
def test_ints_to_pytimedelta(unit):
# tests for non-nanosecond cases
arr = np.arange(6, dtype=np.int64).view(f"m8[{unit}]")
res = ints_to_pytimedelta(arr, box=False)
# For non-nanosecond, .astype(object) gives pytimedelta objects
# instead of integers
expected = arr.astype(object)
tm.assert_numpy_array_equal(res, expected)
res = ints_to_pytimedelta(arr, box=True)
expected = np.array([Timedelta(x) for x in arr], dtype=object)
tm.assert_numpy_array_equal(res, expected)
@pytest.mark.parametrize("unit", ["Y", "M", "ps", "fs", "as"])
def test_ints_to_pytimedelta_unsupported(unit):
arr = np.arange(6, dtype=np.int64).view(f"m8[{unit}]")
with pytest.raises(NotImplementedError, match=r"\d{1,2}"):
ints_to_pytimedelta(arr, box=False)
msg = "Only resolutions 's', 'ms', 'us', 'ns' are supported"
with pytest.raises(NotImplementedError, match=msg):
ints_to_pytimedelta(arr, box=True)