forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_to_offset.py
241 lines (204 loc) · 6.46 KB
/
test_to_offset.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
import re
import pytest
from pandas._libs.tslibs import (
Timedelta,
offsets,
to_offset,
)
import pandas._testing as tm
@pytest.mark.parametrize(
"freq_input,expected",
[
(to_offset("10us"), offsets.Micro(10)),
(offsets.Hour(), offsets.Hour()),
("2h30min", offsets.Minute(150)),
("2h 30min", offsets.Minute(150)),
("2h30min15s", offsets.Second(150 * 60 + 15)),
("2h 60min", offsets.Hour(3)),
("2h 20.5min", offsets.Second(8430)),
("1.5min", offsets.Second(90)),
("0.5s", offsets.Milli(500)),
("15ms500us", offsets.Micro(15500)),
("10s75ms", offsets.Milli(10075)),
("1s0.25ms", offsets.Micro(1000250)),
("2800ns", offsets.Nano(2800)),
("2SME", offsets.SemiMonthEnd(2)),
("2SME-16", offsets.SemiMonthEnd(2, day_of_month=16)),
("2SMS-14", offsets.SemiMonthBegin(2, day_of_month=14)),
("2SMS-15", offsets.SemiMonthBegin(2)),
("LWOM-MON", offsets.LastWeekOfMonth()),
],
)
def test_to_offset(freq_input, expected):
result = to_offset(freq_input)
assert result == expected
@pytest.mark.parametrize(
"freqstr,expected", [("-1s", -1), ("-2SME", -2), ("-1SMS", -1), ("-5min10s", -310)]
)
def test_to_offset_negative(freqstr, expected):
result = to_offset(freqstr)
assert result.n == expected
@pytest.mark.filterwarnings("ignore:.*'m' is deprecated.*:FutureWarning")
@pytest.mark.parametrize(
"freqstr",
[
"2h20m",
"us1",
"-us",
"3us1",
"-2-3us",
"-2D:3h",
"1.5.0s",
"2SMS-15-15",
"2SMS-15D",
"100foo",
# Invalid leading +/- signs.
"+-1D",
"-+1h",
"+1",
"-7",
"+D",
"-m",
# Invalid shortcut anchors.
"SME-0",
"SME-28",
"SME-29",
"SME-FOO",
"BSM",
"SME--1",
"SMS-1",
"SMS-28",
"SMS-30",
"SMS-BAR",
"SMS-BYR",
"BSMS",
"SMS--2",
],
)
def test_to_offset_invalid(freqstr):
# see gh-13930
# We escape string because some of our
# inputs contain regex special characters.
msg = re.escape(f"Invalid frequency: {freqstr}")
with pytest.raises(ValueError, match=msg):
to_offset(freqstr)
def test_to_offset_no_evaluate():
msg = str(("", ""))
with pytest.raises(TypeError, match=msg):
to_offset(("", ""))
def test_to_offset_tuple_unsupported():
with pytest.raises(TypeError, match="pass as a string instead"):
to_offset((5, "T"))
@pytest.mark.parametrize(
"freqstr,expected",
[
("2D 3h", offsets.Hour(51)),
("2 D3 h", offsets.Hour(51)),
("2 D 3 h", offsets.Hour(51)),
(" 2 D 3 h ", offsets.Hour(51)),
(" h ", offsets.Hour()),
(" 3 h ", offsets.Hour(3)),
],
)
def test_to_offset_whitespace(freqstr, expected):
result = to_offset(freqstr)
assert result == expected
@pytest.mark.parametrize(
"freqstr,expected", [("00h 00min 01s", 1), ("-00h 03min 14s", -194)]
)
def test_to_offset_leading_zero(freqstr, expected):
result = to_offset(freqstr)
assert result.n == expected
@pytest.mark.parametrize(
"freqstr,expected,wrn", [("+1d", 1, FutureWarning), ("+2h30min", 150, None)]
)
def test_to_offset_leading_plus(freqstr, expected, wrn):
msg = "'d' is deprecated and will be removed in a future version."
with tm.assert_produces_warning(wrn, match=msg):
result = to_offset(freqstr)
assert result.n == expected
@pytest.mark.parametrize(
"kwargs,expected",
[
({"days": 1, "seconds": 1}, offsets.Second(86401)),
({"days": -1, "seconds": 1}, offsets.Second(-86399)),
({"hours": 1, "minutes": 10}, offsets.Minute(70)),
({"hours": 1, "minutes": -10}, offsets.Minute(50)),
({"weeks": 1}, offsets.Day(7)),
({"hours": 1}, offsets.Hour(1)),
({"hours": 1}, to_offset("60min")),
({"microseconds": 1}, offsets.Micro(1)),
({"microseconds": 0}, offsets.Nano(0)),
],
)
def test_to_offset_pd_timedelta(kwargs, expected):
# see gh-9064
td = Timedelta(**kwargs)
result = to_offset(td)
assert result == expected
@pytest.mark.parametrize(
"shortcut,expected",
[
("W", offsets.Week(weekday=6)),
("W-SUN", offsets.Week(weekday=6)),
("QE", offsets.QuarterEnd(startingMonth=12)),
("QE-DEC", offsets.QuarterEnd(startingMonth=12)),
("QE-MAY", offsets.QuarterEnd(startingMonth=5)),
("SME", offsets.SemiMonthEnd(day_of_month=15)),
("SME-15", offsets.SemiMonthEnd(day_of_month=15)),
("SME-1", offsets.SemiMonthEnd(day_of_month=1)),
("SME-27", offsets.SemiMonthEnd(day_of_month=27)),
("SMS-2", offsets.SemiMonthBegin(day_of_month=2)),
("SMS-27", offsets.SemiMonthBegin(day_of_month=27)),
],
)
def test_anchored_shortcuts(shortcut, expected):
result = to_offset(shortcut)
assert result == expected
@pytest.mark.parametrize(
"freq_depr",
[
"2ye-mar",
"2ys",
"2qe",
"2qs-feb",
"2bqs",
"2sms",
"1sme",
"2bms",
"2cbme",
"2me",
],
)
def test_to_offset_lowercase_frequency_raises(freq_depr):
msg = f"Invalid frequency: {freq_depr}"
with pytest.raises(ValueError, match=msg):
to_offset(freq_depr)
@pytest.mark.parametrize("freq_depr", ["2MIN", "2Us", "2NS"])
def test_to_offset_uppercase_frequency_deprecated(freq_depr):
# GH#54939
depr_msg = (
f"'{freq_depr[1:]}' is deprecated and will be removed in a "
f"future version, please use '{freq_depr.lower()[1:]}' instead."
)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
to_offset(freq_depr)
@pytest.mark.parametrize(
"freq_depr,expected",
[
("2w", offsets.Week(2, weekday=6)),
("2b", offsets.BusinessDay(2)),
("2d", offsets.Day(2)),
],
)
def test_to_offset_lowercase_frequency_deprecated(freq_depr, expected):
# GH#54939, GH#58998
msg = f"'{freq_depr[1:]}' is deprecated and will be removed in a future version."
with tm.assert_produces_warning(FutureWarning, match=msg):
result = to_offset(freq_depr)
assert result == expected
@pytest.mark.parametrize("freq", ["2H", "2BH", "2S"])
def test_to_offset_uppercase_frequency_raises(freq):
msg = f"Invalid frequency: {freq}"
with pytest.raises(ValueError, match=msg):
to_offset(freq)