zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
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
import re
 
import pytest
 
from pandas._libs.tslibs import (
    Timedelta,
    offsets,
    to_offset,
)
 
 
@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)),
        ("15l500u", offsets.Micro(15500)),
        ("10s75L", offsets.Milli(10075)),
        ("1s0.25ms", offsets.Micro(1000250)),
        ("1s0.25L", offsets.Micro(1000250)),
        ("2800N", offsets.Nano(2800)),
        ("2SM", offsets.SemiMonthEnd(2)),
        ("2SM-16", offsets.SemiMonthEnd(2, day_of_month=16)),
        ("2SMS-14", offsets.SemiMonthBegin(2, day_of_month=14)),
        ("2SMS-15", offsets.SemiMonthBegin(2)),
    ],
)
def test_to_offset(freq_input, expected):
    result = to_offset(freq_input)
    assert result == expected
 
 
@pytest.mark.parametrize(
    "freqstr,expected", [("-1S", -1), ("-2SM", -2), ("-1SMS", -1), ("-5min10s", -310)]
)
def test_to_offset_negative(freqstr, expected):
    result = to_offset(freqstr)
    assert result.n == expected
 
 
@pytest.mark.parametrize(
    "freqstr",
    [
        "2h20m",
        "U1",
        "-U",
        "3U1",
        "-2-3U",
        "-2D:3H",
        "1.5.0S",
        "2SMS-15-15",
        "2SMS-15D",
        "100foo",
        # Invalid leading +/- signs.
        "+-1d",
        "-+1h",
        "+1",
        "-7",
        "+d",
        "-m",
        # Invalid shortcut anchors.
        "SM-0",
        "SM-28",
        "SM-29",
        "SM-FOO",
        "BSM",
        "SM--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 00T 01S", 1), ("-00H 03T 14S", -194)]
)
def test_to_offset_leading_zero(freqstr, expected):
    result = to_offset(freqstr)
    assert result.n == expected
 
 
@pytest.mark.parametrize("freqstr,expected", [("+1d", 1), ("+2h30min", 150)])
def test_to_offset_leading_plus(freqstr, expected):
    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)),
        ("Q", offsets.QuarterEnd(startingMonth=12)),
        ("Q-DEC", offsets.QuarterEnd(startingMonth=12)),
        ("Q-MAY", offsets.QuarterEnd(startingMonth=5)),
        ("SM", offsets.SemiMonthEnd(day_of_month=15)),
        ("SM-15", offsets.SemiMonthEnd(day_of_month=15)),
        ("SM-1", offsets.SemiMonthEnd(day_of_month=1)),
        ("SM-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