hyb
2025-11-04 668edf874b4f77214a8ff4513e60e3c1a973f532
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
import numpy as np
import pytest
 
from pandas import (
    Timedelta,
    TimedeltaIndex,
    timedelta_range,
    to_timedelta,
)
import pandas._testing as tm
 
from pandas.tseries.offsets import (
    Day,
    Second,
)
 
 
class TestTimedeltas:
    def test_timedelta_range_unit(self):
        # GH#49824
        tdi = timedelta_range("0 Days", periods=10, freq="100000D", unit="s")
        exp_arr = (np.arange(10, dtype="i8") * 100_000).view("m8[D]").astype("m8[s]")
        tm.assert_numpy_array_equal(tdi.to_numpy(), exp_arr)
 
    def test_timedelta_range(self):
        expected = to_timedelta(np.arange(5), unit="D")
        result = timedelta_range("0 days", periods=5, freq="D")
        tm.assert_index_equal(result, expected)
 
        expected = to_timedelta(np.arange(11), unit="D")
        result = timedelta_range("0 days", "10 days", freq="D")
        tm.assert_index_equal(result, expected)
 
        expected = to_timedelta(np.arange(5), unit="D") + Second(2) + Day()
        result = timedelta_range("1 days, 00:00:02", "5 days, 00:00:02", freq="D")
        tm.assert_index_equal(result, expected)
 
        expected = to_timedelta([1, 3, 5, 7, 9], unit="D") + Second(2)
        result = timedelta_range("1 days, 00:00:02", periods=5, freq="2D")
        tm.assert_index_equal(result, expected)
 
        expected = to_timedelta(np.arange(50), unit="min") * 30
        result = timedelta_range("0 days", freq="30min", periods=50)
        tm.assert_index_equal(result, expected)
 
    @pytest.mark.parametrize(
        "depr_unit, unit",
        [
            ("H", "hour"),
            ("T", "minute"),
            ("t", "minute"),
            ("S", "second"),
            ("L", "millisecond"),
            ("l", "millisecond"),
            ("U", "microsecond"),
            ("u", "microsecond"),
            ("N", "nanosecond"),
            ("n", "nanosecond"),
        ],
    )
    def test_timedelta_units_H_T_S_L_U_N_deprecated(self, depr_unit, unit):
        # GH#52536
        depr_msg = (
            f"'{depr_unit}' is deprecated and will be removed in a future version."
        )
 
        expected = to_timedelta(np.arange(5), unit=unit)
        with tm.assert_produces_warning(FutureWarning, match=depr_msg):
            result = to_timedelta(np.arange(5), unit=depr_unit)
            tm.assert_index_equal(result, expected)
 
    @pytest.mark.parametrize(
        "periods, freq", [(3, "2D"), (5, "D"), (6, "19h12min"), (7, "16h"), (9, "12h")]
    )
    def test_linspace_behavior(self, periods, freq):
        # GH 20976
        result = timedelta_range(start="0 days", end="4 days", periods=periods)
        expected = timedelta_range(start="0 days", end="4 days", freq=freq)
        tm.assert_index_equal(result, expected)
 
    @pytest.mark.parametrize("msg_freq, freq", [("H", "19H12min"), ("T", "19h12T")])
    def test_timedelta_range_H_T_deprecated(self, freq, msg_freq):
        # GH#52536
        msg = f"'{msg_freq}' is deprecated and will be removed in a future version."
 
        result = timedelta_range(start="0 days", end="4 days", periods=6)
        with tm.assert_produces_warning(FutureWarning, match=msg):
            expected = timedelta_range(start="0 days", end="4 days", freq=freq)
        tm.assert_index_equal(result, expected)
 
    def test_errors(self):
        # not enough params
        msg = (
            "Of the four parameters: start, end, periods, and freq, "
            "exactly three must be specified"
        )
        with pytest.raises(ValueError, match=msg):
            timedelta_range(start="0 days")
 
        with pytest.raises(ValueError, match=msg):
            timedelta_range(end="5 days")
 
        with pytest.raises(ValueError, match=msg):
            timedelta_range(periods=2)
 
        with pytest.raises(ValueError, match=msg):
            timedelta_range()
 
        # too many params
        with pytest.raises(ValueError, match=msg):
            timedelta_range(start="0 days", end="5 days", periods=10, freq="h")
 
    @pytest.mark.parametrize(
        "start, end, freq, expected_periods",
        [
            ("1D", "10D", "2D", (10 - 1) // 2 + 1),
            ("2D", "30D", "3D", (30 - 2) // 3 + 1),
            ("2s", "50s", "5s", (50 - 2) // 5 + 1),
            # tests that worked before GH 33498:
            ("4D", "16D", "3D", (16 - 4) // 3 + 1),
            ("8D", "16D", "40s", (16 * 3600 * 24 - 8 * 3600 * 24) // 40 + 1),
        ],
    )
    def test_timedelta_range_freq_divide_end(self, start, end, freq, expected_periods):
        # GH 33498 only the cases where `(end % freq) == 0` used to fail
        res = timedelta_range(start=start, end=end, freq=freq)
        assert Timedelta(start) == res[0]
        assert Timedelta(end) >= res[-1]
        assert len(res) == expected_periods
 
    def test_timedelta_range_infer_freq(self):
        # https://github.com/pandas-dev/pandas/issues/35897
        result = timedelta_range("0s", "1s", periods=31)
        assert result.freq is None
 
    @pytest.mark.parametrize(
        "freq_depr, start, end, expected_values, expected_freq",
        [
            (
                "3.5S",
                "05:03:01",
                "05:03:10",
                ["0 days 05:03:01", "0 days 05:03:04.500000", "0 days 05:03:08"],
                "3500ms",
            ),
            (
                "2.5T",
                "5 hours",
                "5 hours 8 minutes",
                [
                    "0 days 05:00:00",
                    "0 days 05:02:30",
                    "0 days 05:05:00",
                    "0 days 05:07:30",
                ],
                "150s",
            ),
        ],
    )
    def test_timedelta_range_deprecated_freq(
        self, freq_depr, start, end, expected_values, expected_freq
    ):
        # GH#52536
        msg = (
            f"'{freq_depr[-1]}' is deprecated and will be removed in a future version."
        )
 
        with tm.assert_produces_warning(FutureWarning, match=msg):
            result = timedelta_range(start=start, end=end, freq=freq_depr)
        expected = TimedeltaIndex(
            expected_values, dtype="timedelta64[ns]", freq=expected_freq
        )
        tm.assert_index_equal(result, expected)