diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 65982ecdb810c..d0a2ab14d8146 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..e1408e3e41762 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -1420,9 +1420,9 @@ 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)) + isinstance(start, (float, np.floating)) + or isinstance(end, (float, np.floating)) + or isinstance(freq, (float, np.floating)) ): dtype = np.dtype("float64") elif ( diff --git a/pandas/tests/indexes/interval/test_interval_range.py b/pandas/tests/indexes/interval/test_interval_range.py index 5252b85ad8d0e..8cfc9884233a1 100644 --- a/pandas/tests/indexes/interval/test_interval_range.py +++ b/pandas/tests/indexes/interval/test_interval_range.py @@ -225,7 +225,7 @@ def test_float_subtype(self, start, end, freq): [ (np.int8(1), np.int8(10), np.dtype("int8")), (np.int8(1), np.float16(10), np.dtype("float64")), - (np.float32(1), np.float32(10), np.dtype("float32")), + (np.float32(1), np.float32(10), np.dtype("float64")), (1, 10, np.dtype("int64")), (1, 10.0, np.dtype("float64")), ], @@ -380,3 +380,13 @@ 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 + from pandas.testing import assert_index_equal + + 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]" + ) + assert_index_equal(result, expected)