Skip to content

Commit f953112

Browse files
Backport PR #36595: Partial Revert "ENH: infer freq in timedelta_range (#32377)" (#36659)
Co-authored-by: Simon Hawkins <[email protected]>
1 parent c5bd38d commit f953112

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

doc/source/whatsnew/v1.1.3.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Fixed regressions
3131
~~~~~~~~~~~~~~~~~
3232
- Fixed regression in :meth:`DataFrame.agg`, :meth:`DataFrame.apply`, :meth:`Series.agg`, and :meth:`Series.apply` where internal suffix is exposed to the users when no relabelling is applied (:issue:`36189`)
3333
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)
34+
- Fixed regression when adding a :meth:`timedelta_range` to a :class:``Timestamp`` raised an ``ValueError`` (:issue:`35897`)
3435
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
3536
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
3637
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`)
@@ -62,7 +63,7 @@ Bug fixes
6263

6364
Other
6465
~~~~~
65-
-
66+
- Reverted enhancement added in pandas-1.1.0 where :func:`timedelta_range` infers a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`)
6667

6768
.. ---------------------------------------------------------------------------
6869

pandas/core/arrays/timedeltas.py

-4
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
256256
index = generate_regular_range(start, end, periods, freq)
257257
else:
258258
index = np.linspace(start.value, end.value, periods).astype("i8")
259-
if len(index) >= 2:
260-
# Infer a frequency
261-
td = Timedelta(index[1] - index[0])
262-
freq = to_offset(td)
263259

264260
if not left_closed:
265261
index = index[1:]

pandas/core/indexes/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ def timedelta_range(
323323
324324
>>> pd.timedelta_range(start='1 day', end='5 days', periods=4)
325325
TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00',
326-
'5 days 00:00:00'],
327-
dtype='timedelta64[ns]', freq='32H')
326+
'5 days 00:00:00'],
327+
dtype='timedelta64[ns]', freq=None)
328328
"""
329329
if freq is None and com.any_none(periods, start, end):
330330
freq = "D"

pandas/tests/arithmetic/test_timedelta64.py

+17
Original file line numberDiff line numberDiff line change
@@ -2119,3 +2119,20 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):
21192119

21202120
with pytest.raises(TypeError, match=pattern):
21212121
td1 ** scalar_td
2122+
2123+
2124+
def test_add_timestamp_to_timedelta():
2125+
# GH: 35897
2126+
timestamp = pd.Timestamp.now()
2127+
result = timestamp + pd.timedelta_range("0s", "1s", periods=31)
2128+
expected = pd.DatetimeIndex(
2129+
[
2130+
timestamp
2131+
+ (
2132+
pd.to_timedelta("0.033333333s") * i
2133+
+ pd.to_timedelta("0.000000001s") * divmod(i, 3)[0]
2134+
)
2135+
for i in range(31)
2136+
]
2137+
)
2138+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/timedeltas/test_timedelta_range.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def test_linspace_behavior(self, periods, freq):
3838
result = timedelta_range(start="0 days", end="4 days", periods=periods)
3939
expected = timedelta_range(start="0 days", end="4 days", freq=freq)
4040
tm.assert_index_equal(result, expected)
41-
assert result.freq == freq
4241

4342
def test_errors(self):
4443
# not enough params
@@ -79,3 +78,8 @@ def test_timedelta_range_freq_divide_end(self, start, end, freq, expected_period
7978
assert Timedelta(start) == res[0]
8079
assert Timedelta(end) >= res[-1]
8180
assert len(res) == expected_periods
81+
82+
def test_timedelta_range_infer_freq(self):
83+
# https://github.com/pandas-dev/pandas/issues/35897
84+
result = timedelta_range("0s", "1s", periods=31)
85+
assert result.freq is None

0 commit comments

Comments
 (0)