|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pandas._libs.tslibs import frequencies as libfrequencies, resolution |
| 4 | +from pandas._libs.tslibs.frequencies import ( |
| 5 | + FreqGroup, _period_code_map, get_freq, get_freq_code) |
| 6 | +import pandas.compat as compat |
| 7 | + |
| 8 | +import pandas.tseries.offsets as offsets |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(params=list(compat.iteritems(_period_code_map))) |
| 12 | +def period_code_item(request): |
| 13 | + return request.param |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("freqstr,expected", [ |
| 17 | + ("A", 1000), ("3A", 1000), ("-1A", 1000), |
| 18 | + ("Y", 1000), ("3Y", 1000), ("-1Y", 1000), |
| 19 | + ("W", 4000), ("W-MON", 4001), ("W-FRI", 4005) |
| 20 | +]) |
| 21 | +def test_freq_code(freqstr, expected): |
| 22 | + assert get_freq(freqstr) == expected |
| 23 | + |
| 24 | + |
| 25 | +def test_freq_code_match(period_code_item): |
| 26 | + freqstr, code = period_code_item |
| 27 | + assert get_freq(freqstr) == code |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("freqstr,expected", [ |
| 31 | + ("A", 1000), ("3A", 1000), ("-1A", 1000), ("A-JAN", 1000), |
| 32 | + ("A-MAY", 1000), ("Y", 1000), ("3Y", 1000), ("-1Y", 1000), |
| 33 | + ("Y-JAN", 1000), ("Y-MAY", 1000), (offsets.YearEnd(), 1000), |
| 34 | + (offsets.YearEnd(month=1), 1000), (offsets.YearEnd(month=5), 1000), |
| 35 | + ("W", 4000), ("W-MON", 4000), ("W-FRI", 4000), (offsets.Week(), 4000), |
| 36 | + (offsets.Week(weekday=1), 4000), (offsets.Week(weekday=5), 4000), |
| 37 | + ("T", FreqGroup.FR_MIN), |
| 38 | +]) |
| 39 | +def test_freq_group(freqstr, expected): |
| 40 | + assert resolution.get_freq_group(freqstr) == expected |
| 41 | + |
| 42 | + |
| 43 | +def test_freq_group_match(period_code_item): |
| 44 | + freqstr, code = period_code_item |
| 45 | + |
| 46 | + str_group = resolution.get_freq_group(freqstr) |
| 47 | + code_group = resolution.get_freq_group(code) |
| 48 | + |
| 49 | + assert str_group == code_group == code // 1000 * 1000 |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("freqstr,exp_freqstr", [ |
| 53 | + ("D", "D"), ("W", "D"), ("M", "D"), |
| 54 | + ("S", "S"), ("T", "S"), ("H", "S") |
| 55 | +]) |
| 56 | +def test_get_to_timestamp_base(freqstr, exp_freqstr): |
| 57 | + tsb = libfrequencies.get_to_timestamp_base |
| 58 | + |
| 59 | + assert tsb(get_freq_code(freqstr)[0]) == get_freq_code(exp_freqstr)[0] |
| 60 | + |
| 61 | + |
| 62 | +_reso = resolution.Resolution |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("freqstr,expected", [ |
| 66 | + ("A", "year"), ("Q", "quarter"), ("M", "month"), |
| 67 | + ("D", "day"), ("H", "hour"), ("T", "minute"), |
| 68 | + ("S", "second"), ("L", "millisecond"), |
| 69 | + ("U", "microsecond"), ("N", "nanosecond") |
| 70 | +]) |
| 71 | +def test_get_str_from_freq(freqstr, expected): |
| 72 | + assert _reso.get_str_from_freq(freqstr) == expected |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("freq", ["A", "Q", "M", "D", "H", |
| 76 | + "T", "S", "L", "U", "N"]) |
| 77 | +def test_get_freq_roundtrip(freq): |
| 78 | + result = _reso.get_freq(_reso.get_str_from_freq(freq)) |
| 79 | + assert freq == result |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U"]) |
| 83 | +def test_get_freq_roundtrip2(freq): |
| 84 | + result = _reso.get_freq(_reso.get_str(_reso.get_reso_from_freq(freq))) |
| 85 | + assert freq == result |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("args,expected", [ |
| 89 | + ((1.5, "T"), (90, "S")), ((62.4, "T"), (3744, "S")), |
| 90 | + ((1.04, "H"), (3744, "S")), ((1, "D"), (1, "D")), |
| 91 | + ((0.342931, "H"), (1234551600, "U")), ((1.2345, "D"), (106660800, "L")) |
| 92 | +]) |
| 93 | +def test_resolution_bumping(args, expected): |
| 94 | + # see gh-14378 |
| 95 | + assert _reso.get_stride_from_decimal(*args) == expected |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("args", [ |
| 99 | + (0.5, "N"), |
| 100 | +
|
| 101 | + # Too much precision in the input can prevent. |
| 102 | + (0.3429324798798269273987982, "H") |
| 103 | +]) |
| 104 | +def test_cat(args): |
| 105 | + msg = "Could not convert to integer offset at any resolution" |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match=msg): |
| 108 | + _reso.get_stride_from_decimal(*args) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize("freq_input,expected", [ |
| 112 | + # Frequency string. |
| 113 | + ("A", (get_freq("A"), 1)), |
| 114 | + ("3D", (get_freq("D"), 3)), |
| 115 | + ("-2M", (get_freq("M"), -2)), |
| 116 | +
|
| 117 | + # Tuple. |
| 118 | + (("D", 1), (get_freq("D"), 1)), |
| 119 | + (("A", 3), (get_freq("A"), 3)), |
| 120 | + (("M", -2), (get_freq("M"), -2)), |
| 121 | + ((5, "T"), (FreqGroup.FR_MIN, 5)), |
| 122 | +
|
| 123 | + # Numeric Tuple. |
| 124 | + ((1000, 1), (1000, 1)), |
| 125 | +
|
| 126 | + # Offsets. |
| 127 | + (offsets.Day(), (get_freq("D"), 1)), |
| 128 | + (offsets.Day(3), (get_freq("D"), 3)), |
| 129 | + (offsets.Day(-2), (get_freq("D"), -2)), |
| 130 | + (offsets.MonthEnd(), (get_freq("M"), 1)), |
| 131 | + (offsets.MonthEnd(3), (get_freq("M"), 3)), |
| 132 | + (offsets.MonthEnd(-2), (get_freq("M"), -2)), |
| 133 | + (offsets.Week(), (get_freq("W"), 1)), |
| 134 | + (offsets.Week(3), (get_freq("W"), 3)), |
| 135 | + (offsets.Week(-2), (get_freq("W"), -2)), |
| 136 | + (offsets.Hour(), (FreqGroup.FR_HR, 1)), |
| 137 | +
|
| 138 | + # Monday is weekday=0. |
| 139 | + (offsets.Week(weekday=1), (get_freq("W-TUE"), 1)), |
| 140 | + (offsets.Week(3, weekday=0), (get_freq("W-MON"), 3)), |
| 141 | + (offsets.Week(-2, weekday=4), (get_freq("W-FRI"), -2)), |
| 142 | +]) |
| 143 | +def test_get_freq_code(freq_input, expected): |
| 144 | + assert get_freq_code(freq_input) == expected |
| 145 | + |
| 146 | + |
| 147 | +def test_get_code_invalid(): |
| 148 | + with pytest.raises(ValueError, match="Invalid frequency"): |
| 149 | + get_freq_code((5, "baz")) |
0 commit comments