BUG: Fix a bug in 'timedelta_range' that produced an extra point on a edge case (fix #30353) by hasB4K · Pull Request #33498 · pandas-dev/pandas (original) (raw)

The issue from #30353 came actually from timedelta_range.

import pandas as pd

def mock_timedelta_range(start=None, end=None, periods=None, freq=None, name=None, closed=None): epoch = pd.Timestamp(0) if start is not None: start = epoch + pd.Timedelta(start) if end is not None: end = epoch + pd.Timedelta(end) res = pd.date_range(start=start, end=end, periods=periods, freq=freq, name=name, closed=closed) res -= epoch res.freq = freq return res

print(mock_timedelta_range("1day", "10day", freq="2D")) print(pd.timedelta_range("1day", "10day", freq="2D"))

The outputs from mock_timedelta_range and from pd.timedelta_range are supposed to equivalent, but are not on pandas v1.0.0:
Outputs without this PR:

TimedeltaIndex(['1 days', '3 days', '5 days', '7 days', '9 days'], dtype='timedelta64[ns]', freq='2D')
TimedeltaIndex(['1 days', '3 days', '5 days', '7 days', '9 days', '11 days'], dtype='timedelta64[ns]', freq='2D')

Outputs with this PR:

TimedeltaIndex(['1 days', '3 days', '5 days', '7 days', '9 days'], dtype='timedelta64[ns]', freq='2D')
TimedeltaIndex(['1 days', '3 days', '5 days', '7 days', '9 days'], dtype='timedelta64[ns]', freq='2D')

It also solve an issue (that fail on master) related to this comment: #13022 (comment)

import pandas as pd rng = pd.timedelta_range(start='1d', periods=10, freq='d') df = pd.Series(range(10), index=rng) df.resample('2D').count()

Outputs with this PR:

1 days    2
3 days    2
5 days    2
7 days    2
9 days    2
Freq: 2D, dtype: int64

On the firsts commits I just fixed the issue in _generate_regular_range, then I decided to do a refactor and to use the same code that generate ranges in core/arrays/_ranges.py for date_range and timedelta_range.

Linked with #10887.