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
| import numpy as np
| import pytest
|
| from pandas._libs.tslibs import (
| Period,
| Resolution,
| to_offset,
| )
| from pandas._libs.tslibs.dtypes import _attrname_to_abbrevs
|
|
| @pytest.mark.parametrize(
| "freqstr,exp_freqstr",
| [("D", "D"), ("W", "D"), ("M", "D"), ("S", "S"), ("T", "S"), ("H", "S")],
| )
| def test_get_to_timestamp_base(freqstr, exp_freqstr):
| off = to_offset(freqstr)
| per = Period._from_ordinal(1, off)
| exp_code = to_offset(exp_freqstr)._period_dtype_code
|
| result_code = per._dtype._get_to_timestamp_base()
| assert result_code == exp_code
|
|
| @pytest.mark.parametrize(
| "freqstr,expected",
| [
| ("A", "year"),
| ("Q", "quarter"),
| ("M", "month"),
| ("D", "day"),
| ("H", "hour"),
| ("T", "minute"),
| ("S", "second"),
| ("L", "millisecond"),
| ("U", "microsecond"),
| ("N", "nanosecond"),
| ],
| )
| def test_get_attrname_from_abbrev(freqstr, expected):
| assert Resolution.get_reso_from_freqstr(freqstr).attrname == expected
|
|
| @pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U", "N"])
| def test_get_freq_roundtrip2(freq):
| obj = Resolution.get_reso_from_freqstr(freq)
| result = _attrname_to_abbrevs[obj.attrname]
| assert freq == result
|
|
| @pytest.mark.parametrize(
| "args,expected",
| [
| ((1.5, "T"), (90, "S")),
| ((62.4, "T"), (3744, "S")),
| ((1.04, "H"), (3744, "S")),
| ((1, "D"), (1, "D")),
| ((0.342931, "H"), (1234551600, "U")),
| ((1.2345, "D"), (106660800, "L")),
| ],
| )
| def test_resolution_bumping(args, expected):
| # see gh-14378
| off = to_offset(str(args[0]) + args[1])
| assert off.n == expected[0]
| assert off._prefix == expected[1]
|
|
| @pytest.mark.parametrize(
| "args",
| [
| (0.5, "N"),
| # Too much precision in the input can prevent.
| (0.3429324798798269273987982, "H"),
| ],
| )
| def test_cat(args):
| msg = "Invalid frequency"
|
| with pytest.raises(ValueError, match=msg):
| to_offset(str(args[0]) + args[1])
|
|
| @pytest.mark.parametrize(
| "freqstr,expected",
| [
| ("1H", "2021-01-01T09:00:00"),
| ("1D", "2021-01-02T08:00:00"),
| ("1W", "2021-01-03T08:00:00"),
| ("1M", "2021-01-31T08:00:00"),
| ("1Y", "2021-12-31T08:00:00"),
| ],
| )
| def test_compatibility(freqstr, expected):
| ts_np = np.datetime64("2021-01-01T08:00:00.00")
| do = to_offset(freqstr)
| assert ts_np + do == np.datetime64(expected)
|
|