Skip to content

BUG: Fix interval_range when start/periods or end/periods are specified with float start/end #21162

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
merged 1 commit into from
May 23, 2018
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Indexing
^^^^^^^^

- Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`)
- Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`)
-

I/O
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,10 @@ def interval_range(start=None, end=None, periods=None, freq=None,
periods += 1

if is_number(endpoint):
# force consistency between start/end/freq (lower end if freq skips it)
if com._all_not_none(start, end, freq):
end -= (end - start) % freq

# compute the period/start/end if unspecified (at most one)
if periods is None:
periods = int((end - start) // freq) + 1
Expand All @@ -1580,10 +1584,6 @@ def interval_range(start=None, end=None, periods=None, freq=None,
elif end is None:
end = start + (periods - 1) * freq

# force end to be consistent with freq (lower if freq skips end)
if freq is not None:
end -= end % freq

breaks = np.linspace(start, end, periods)
if all(is_integer(x) for x in com._not_none(start, end, freq)):
# np.linspace always produces float output
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexes/interval/test_interval_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def test_constructor_timedelta(self, closed, name, freq, periods):

@pytest.mark.parametrize('start, end, freq, expected_endpoint', [
(0, 10, 3, 9),
(0, 10, 1.5, 9),
(0.5, 10, 3, 9.5),
(Timedelta('0D'), Timedelta('10D'), '2D4H', Timedelta('8D16H')),
(Timestamp('2018-01-01'),
Timestamp('2018-02-09'),
Expand All @@ -125,6 +127,22 @@ def test_early_truncation(self, start, end, freq, expected_endpoint):
result_endpoint = result.right[-1]
assert result_endpoint == expected_endpoint

@pytest.mark.parametrize('start, end, freq', [
(0.5, None, None),
(None, 4.5, None),
(0.5, None, 1.5),
(None, 6.5, 1.5)])
def test_no_invalid_float_truncation(self, start, end, freq):
# GH 21161
if freq is None:
breaks = [0.5, 1.5, 2.5, 3.5, 4.5]
else:
breaks = [0.5, 2.0, 3.5, 5.0, 6.5]
expected = IntervalIndex.from_breaks(breaks)

result = interval_range(start=start, end=end, periods=4, freq=freq)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize('start, mid, end', [
(Timestamp('2018-03-10', tz='US/Eastern'),
Timestamp('2018-03-10 23:30:00', tz='US/Eastern'),
Expand Down