diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index ee5085fd9ad89..975909686c28f 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -121,7 +121,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ -- +- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`) - Timedelta diff --git a/pandas/tests/tseries/frequencies/test_inference.py b/pandas/tests/tseries/frequencies/test_inference.py index 396cb950bd8b2..ea9a09ff2d65c 100644 --- a/pandas/tests/tseries/frequencies/test_inference.py +++ b/pandas/tests/tseries/frequencies/test_inference.py @@ -358,9 +358,16 @@ def test_non_datetime_index2(): @pytest.mark.parametrize( - "idx", [tm.makeIntIndex(10), tm.makeFloatIndex(10), tm.makePeriodIndex(10)] + "idx", + [ + tm.makeIntIndex(10), + tm.makeFloatIndex(10), + tm.makePeriodIndex(10), + tm.makeRangeIndex(10), + ], ) def test_invalid_index_types(idx): + # see gh-48439 msg = "|".join( [ "cannot infer freq from a non-convertible", diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 3edabc7c089e1..ce62a15eda367 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -160,6 +160,7 @@ def infer_freq(index, warn: bool = True) -> str | None: Float64Index, Index, Int64Index, + RangeIndex, ) if isinstance(index, ABCSeries): @@ -190,7 +191,7 @@ def infer_freq(index, warn: bool = True) -> str | None: return inferer.get_freq() if isinstance(index, Index) and not isinstance(index, DatetimeIndex): - if isinstance(index, (Int64Index, Float64Index)): + if isinstance(index, (Int64Index, Float64Index, RangeIndex)): raise TypeError( f"cannot infer freq from a non-convertible index type {type(index)}" )