Repository: pandas-dev/pandas

Issue:
BUG: interval_range is incorrect when start/periods or end/periods is specified with float start/end
#### Code Sample, a copy-pastable example if possible

```python
In [2]: pd.interval_range(start=0.5, periods=4)
Out[2]:
IntervalIndex([(0.5, 1.375], (1.375, 2.25], (2.25, 3.125], (3.125, 4.0]]
              closed='right',
              dtype='interval[float64]')

In [3]: pd.interval_range(end=4.5, periods=4)
Out[3]:
IntervalIndex([(0.5, 1.375], (1.375, 2.25], (2.25, 3.125], (3.125, 4.0]]
              closed='right',
              dtype='interval[float64]')
```
#### Problem description
In `[2]` the default frequency should be 1 since `freq` is not specified but instead is 0.875.

In `[3]` the `end=4.5` is not being set correctly, as well as having the same issue with `freq` as `[2]`.

This appears to be caused by overeager truncation logic for when `start`/`end` are both specified and inconsistent with `freq`:

https://github.com/pandas-dev/pandas/blob/172ab7aee433fed7780bf96f0c723af284755324/pandas/core/indexes/interval.py#L1583-L1585

#### Expected Output
I'd expect both outputs to be:
```python
IntervalIndex([(0.5, 1.5], (1.5, 2.5], (2.5, 3.5], (3.5, 4.5]]
              closed='right',
              dtype='interval[float64]')
```
#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: e6864b5cfb2763434b625c1303c6e62bffd1481c
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.0.dev0+12.ge6864b5
pytest: 3.1.2
pip: 9.0.1
setuptools: 39.0.1
Cython: 0.28.2
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.5
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py
--- a/pandas/core/indexes/interval.py
+++ b/pandas/core/indexes/interval.py
@@ -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
@@ -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


Test patch:
diff --git a/pandas/tests/indexes/interval/test_interval_range.py b/pandas/tests/indexes/interval/test_interval_range.py
--- a/pandas/tests/indexes/interval/test_interval_range.py
+++ b/pandas/tests/indexes/interval/test_interval_range.py
@@ -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'),
@@ -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'),