Skip to content

Backport PR #36595 on branch 1.1.x (Partial Revert "ENH: infer freq in timedelta_range (#32377)") #36659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- 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`)
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)
- Fixed regression when adding a :meth:`timedelta_range` to a :class:``Timestamp`` raised an ``ValueError`` (:issue:`35897`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`)
Expand Down Expand Up @@ -61,7 +62,7 @@ Bug fixes

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

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 0 additions & 4 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
index = generate_regular_range(start, end, periods, freq)
else:
index = np.linspace(start.value, end.value, periods).astype("i8")
if len(index) >= 2:
# Infer a frequency
td = Timedelta(index[1] - index[0])
freq = to_offset(td)

if not left_closed:
index = index[1:]
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def timedelta_range(

>>> pd.timedelta_range(start='1 day', end='5 days', periods=4)
TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00',
'5 days 00:00:00'],
dtype='timedelta64[ns]', freq='32H')
'5 days 00:00:00'],
dtype='timedelta64[ns]', freq=None)
"""
if freq is None and com.any_none(periods, start, end):
freq = "D"
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2119,3 +2119,20 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):

with pytest.raises(TypeError, match=pattern):
td1 ** scalar_td


def test_add_timestamp_to_timedelta():
# GH: 35897
timestamp = pd.Timestamp.now()
result = timestamp + pd.timedelta_range("0s", "1s", periods=31)
expected = pd.DatetimeIndex(
[
timestamp
+ (
pd.to_timedelta("0.033333333s") * i
+ pd.to_timedelta("0.000000001s") * divmod(i, 3)[0]
)
for i in range(31)
]
)
tm.assert_index_equal(result, expected)
6 changes: 5 additions & 1 deletion pandas/tests/indexes/timedeltas/test_timedelta_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_linspace_behavior(self, periods, freq):
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)
assert result.freq == freq

def test_errors(self):
# not enough params
Expand Down Expand Up @@ -79,3 +78,8 @@ def test_timedelta_range_freq_divide_end(self, start, end, freq, expected_period
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