diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 0f7be8cfbcb68..b121c5da435b6 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1127,6 +1127,7 @@ Interval - :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`) - 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`) - Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`) +- Bug in :func:`pandas.interval_range` incorrectly inferring ``int64`` dtype when ``np.float32`` and ``int`` are used for ``start`` and ``freq`` (:issue:`58964`) - Bug in :meth:`IntervalIndex.get_indexer` and :meth:`IntervalIndex.drop` when one of the sides of the index is non-unique (:issue:`52245`) - 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`) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index a4a6230337add..37b1838665ee9 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -1420,17 +1420,17 @@ def interval_range( dtype: np.dtype = np.dtype("int64") if com.all_not_none(start, end, freq): if ( - isinstance(start, (float, np.float16)) - or isinstance(end, (float, np.float16)) - or isinstance(freq, (float, np.float16)) - ): - dtype = np.dtype("float64") - elif ( isinstance(start, (np.integer, np.floating)) and isinstance(end, (np.integer, np.floating)) and start.dtype == end.dtype ): dtype = start.dtype + elif ( + isinstance(start, (float, np.floating)) + or isinstance(end, (float, np.floating)) + or isinstance(freq, (float, np.floating)) + ): + dtype = np.dtype("float64") # 0.1 ensures we capture end breaks = np.arange(start, end + (freq * 0.1), freq) breaks = maybe_downcast_numeric(breaks, dtype) diff --git a/pandas/tests/indexes/interval/test_interval_range.py b/pandas/tests/indexes/interval/test_interval_range.py index 5252b85ad8d0e..66c0111edb16e 100644 --- a/pandas/tests/indexes/interval/test_interval_range.py +++ b/pandas/tests/indexes/interval/test_interval_range.py @@ -380,3 +380,11 @@ def test_float_freq(self): result = interval_range(0, 1, freq=0.6) expected = IntervalIndex.from_breaks([0, 0.6]) tm.assert_index_equal(result, expected) + + def test_interval_range_float32_start_int_freq(self): + # GH 58964 + result = interval_range(start=np.float32(0), end=2, freq=1) + expected = IntervalIndex.from_tuples( + [(0.0, 1.0), (1.0, 2.0)], dtype="interval[float64, right]" + ) + tm.assert_index_equal(result, expected)