Skip to content

Commit c6ca221

Browse files
authored
Fix: interval_range infers incorrect integer dtype for np.float32 input (#63052)
1 parent ff8d694 commit c6ca221

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ Interval
11281128
- :meth:`Index.is_monotonic_decreasing`, :meth:`Index.is_monotonic_increasing`, and :meth:`Index.is_unique` could incorrectly be ``False`` for an ``Index`` created from a slice of another ``Index``. (:issue:`57911`)
11291129
- Bug in :class:`Index`, :class:`Series`, :class:`DataFrame` constructors when given a sequence of :class:`Interval` subclass objects casting them to :class:`Interval` (:issue:`46945`)
11301130
- Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`)
1131+
- Bug in :func:`pandas.interval_range` incorrectly inferring ``int64`` dtype when ``np.float32`` and ``int`` are used for ``start`` and ``freq`` (:issue:`58964`)
11311132
- Bug in :meth:`IntervalIndex.get_indexer` and :meth:`IntervalIndex.drop` when one of the sides of the index is non-unique (:issue:`52245`)
11321133
- Construction of :class:`IntervalArray` and :class:`IntervalIndex` from arrays with mismatched signed/unsigned integer dtypes (e.g., ``int64`` and ``uint64``) now raises a :exc:`TypeError` instead of proceeding silently. (:issue:`55715`)
11331134

pandas/core/indexes/interval.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,17 +1420,17 @@ def interval_range(
14201420
dtype: np.dtype = np.dtype("int64")
14211421
if com.all_not_none(start, end, freq):
14221422
if (
1423-
isinstance(start, (float, np.float16))
1424-
or isinstance(end, (float, np.float16))
1425-
or isinstance(freq, (float, np.float16))
1426-
):
1427-
dtype = np.dtype("float64")
1428-
elif (
14291423
isinstance(start, (np.integer, np.floating))
14301424
and isinstance(end, (np.integer, np.floating))
14311425
and start.dtype == end.dtype
14321426
):
14331427
dtype = start.dtype
1428+
elif (
1429+
isinstance(start, (float, np.floating))
1430+
or isinstance(end, (float, np.floating))
1431+
or isinstance(freq, (float, np.floating))
1432+
):
1433+
dtype = np.dtype("float64")
14341434
# 0.1 ensures we capture end
14351435
breaks = np.arange(start, end + (freq * 0.1), freq)
14361436
breaks = maybe_downcast_numeric(breaks, dtype)

pandas/tests/indexes/interval/test_interval_range.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,11 @@ def test_float_freq(self):
380380
result = interval_range(0, 1, freq=0.6)
381381
expected = IntervalIndex.from_breaks([0, 0.6])
382382
tm.assert_index_equal(result, expected)
383+
384+
def test_interval_range_float32_start_int_freq(self):
385+
# GH 58964
386+
result = interval_range(start=np.float32(0), end=2, freq=1)
387+
expected = IntervalIndex.from_tuples(
388+
[(0.0, 1.0), (1.0, 2.0)], dtype="interval[float64, right]"
389+
)
390+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)